您的当前位置:首页正文

Windows多线程编程

2024-11-09 来源:个人技术集锦
一、 实验内容或题目:

以多线程编程的方式完成:
1)随机生成一个数组,求其平均值
2)随机生成一个数组,求其最大值
3)随机生成一个数组,求其最小值

二、 实验目的与要求:

上传的multi-thread.c已经以单线程的方式完成了上述工作。
请将之更改成多线程完成工作的方式,在数组长度较大的情况下,并观察单线程与多线程方式之间的时间对比。

三、 实验步骤:

需要使用的api:
_beginthread, CreateThread, WaitForSingleObject

四、 实验结果:

单进程

五、 总结:

根据多次实验的结果,多线程所用的时间明显要小于单线程,多线程可同时运行多个程序,一定程度上提高了响应速度,更能充分利用CPU。

拓展思考
源码
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> 
#include <process.h>
#include <math.h>
#include <time.h>
#define Thread   __declspec( thread )

Thread int* pNumbers = NULL;

void generateNumbers(unsigned int cardinality) {
	pNumbers = (int*)malloc(cardinality * sizeof(unsigned int));
	assert(pNumbers);
	unsigned int iter;

	srand((unsigned int)time(NULL));
	for (iter = 0; iter < cardinality; iter++) {
		pNumbers[iter] = rand();
	}

	return;
}

void getMax(unsigned int cardinality) {
	unsigned int iter;
	unsigned int max = 0;
	for (iter = 0; iter < cardinality; iter++) {
		if (pNumbers[iter] > max) {
			max = pNumbers[iter];
		}
	}
	printf("Max is %d\n", max);
}

void getMin(unsigned int cardinality) {
	unsigned int iter;
	unsigned int min = UINT_MAX;
	for (iter = 0; iter < cardinality; iter++) {
		if (pNumbers[iter] < min) {
			min = pNumbers[iter];
		}
	}
	printf("Min is %d\n", min);
}

void getAvg(unsigned int cardinality) {
	unsigned int iter;
	unsigned long long sum = 0;
	for (iter = 0; iter < cardinality; iter++) {
		sum += pNumbers[iter];
	}
	printf("Average is %f\n", (float)(sum / cardinality));
}

unsigned int TimeOfMin, TimeOfMax, TimeOfAverage;

int main(int argc, char* argv[]) {
	if (argc < 2) {
		printf("input the cardinality of numbers to be processed.\n");
		return -1;
	}

	int cardinality = atoi(argv[1]);
	if (cardinality <= 0) {
		printf("please input a positive number as cardinality.\n");
		return -1;
	}
	else if (cardinality > 99999999) {
		printf("cardinality is too big to process.\n");
		return -1;
	}

	clock_t start = clock();

	{
		printf("creating %d numbers randomly\n", cardinality);
		generateNumbers(cardinality);
		getMax(cardinality);

		printf("creating %d numbers randomly\n", cardinality);
		generateNumbers(cardinality);
		getMin(cardinality);

		printf("creating %d numbers randomly\n", cardinality);
		generateNumbers(cardinality);
		getAvg(cardinality);
	}

	clock_t finish = clock();
	double duration = (double)(finish - start);

	/
	printf("Time collapsed: %f ms\n", duration);

	free(pNumbers);
	return 0;
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> 
#include <process.h>
#include <math.h>
#include <time.h>
#define Thread __declspec( thread )

Thread int* pNumbers = NULL;

void generateNumbers(unsigned int cardinality) {
	pNumbers = (int*)malloc(cardinality * sizeof(unsigned int));
	assert(pNumbers);
	unsigned int iter;

	srand((unsigned int)time(NULL));
	for (iter = 0; iter < cardinality; iter++) {
		pNumbers[iter] = rand();
	}
	return;
}

void getMax(unsigned int cardinality) {
	unsigned int iter;
	unsigned int max = 0;
	generateNumbers(cardinality);
	for (iter = 0; iter < cardinality; iter++) {
		if (pNumbers[iter] > max) {
			max = pNumbers[iter];
		}
	}
	printf("Max is %d\n", max);
}
//当函数不允许接受参数时,必须使用void限定
void getMin(void* lpVoid) {
	unsigned int iter;
	unsigned int min = UINT_MAX;
	//获得cardinality
	unsigned int cardinality = (int)lpVoid;
	generateNumbers(cardinality);
	for (iter = 0; iter < cardinality; iter++) {
		if (pNumbers[iter] < min) {
			min = pNumbers[iter];
		}
	}
	printf("Min is %d\n", min);
}

void getAvg(void* lpVoid) {
	unsigned int iter;
	unsigned long long sum = 0;
	unsigned int cardinality = (int)lpVoid;
	generateNumbers(cardinality);
	for (iter = 0; iter < cardinality; iter++) {
		sum += pNumbers[iter];
	}
	printf("Average is %f\n", (float)(sum / cardinality));
}

unsigned int TimeOfMin, TimeOfMax, TimeOfAverage;

int main(int argc, char* argv[]) {
	HANDLE hThread1, hThread2;

	if (argc < 2) {
		printf("input the cardinality of numbers to be processed.\n");
		return -1;
	}

	int cardinality = atoi(argv[1]);
	if (cardinality <= 0) {
		printf("please input a positive number as cardinality.\n");
		return -1;
	}
	else if (cardinality > 99999999) {
		printf("cardinality is too big to process.\n");
		return -1;
	}

	clock_t start = clock();

	{
		//主线程
		printf("creating %d numbers randomly\n", cardinality);
		getMax(cardinality);

		//子线程1
		printf("creating %d numbers randomly\n", cardinality);
		HANDLE hThread1 = (HANDLE)_beginthread(getMin, 0, (void*)cardinality);
		WaitForSingleObject(hThread1, INFINITE);

		//子线程2
		printf("creating %d numbers randomly\n", cardinality);
		HANDLE hThread2 = (HANDLE)_beginthread(getAvg, 0, (void*)cardinality);
		WaitForSingleObject(hThread2, INFINITE);
	}

	clock_t finish = clock();
	double duration = (double)(finish - start);
	printf("Time collapsed: %f ms\n", duration);
	free(pNumbers);
	_endthread();
	return 0;
}
显示全文