多线程(三):动态任务划分&静态任务划分

首先这是老师给出的定义——

静态任务划分:问题划分成相对独立的与CUP个数相等的子问题;

动态任务划分:问题具有n(>CUP个数)个相对独立的子问题时,尚未处理的子问题分配给空闲线程。

用实例来说明,这个例子实现了静态任务划分,而前几篇文章的例子都属于动态任务划分。

#include <stdio.h>
#include <pthread.h>

typedef struct myTestType
{
	int threadID;
	int threadNum;
	int dataNum;

	int *input;
	int *output;
	int *index;
}myTest;

int calculate(int input) {
	int i;
	int output = 0;
	for(i=2; i<input/2; i++) {
		if(input % i == 0) {
			output = 1;
			break;
		}
	}
	if(output == 0)
	{
		sleep(1);
	}
	return output;
}

void thread(myTest * pMyTest) {
	printf("Begin threadID=%u run!\n", pMyTest->threadID);
	int input, output;
	int threadID = pMyTest->threadID;
	int threadNum = pMyTest->threadNum;
	int dataNum = pMyTest->dataNum;
	int split = dataNum / threadNum;
	int firstIndex = split * threadID;
	int lastIndex = firstIndex + split;
	if(lastIndex > dataNum)
		lastIndex = dataNum;
	while(firstIndex < lastIndex) {
		input = pMyTest->input[firstIndex];
		output = calculate(input);
		printf("index=%3u, input=%8u, output=%2u, threadID=%2u\n", firstIndex, input, output, threadID);
		pMyTest->output[firstIndex] = output;
		firstIndex++;
	}
	pthread_exit(NULL);
}

int main(void) {
	int i, ret;
	int threadNum = 2;
	myTest * pMyTest = (myTest *)malloc(sizeof(myTest));
	pMyTest->dataNum = 100;
	pMyTest->input = (int *)malloc(sizeof(int)*pMyTest->dataNum);
	pMyTest->output = (int *)malloc(sizeof(int)*pMyTest->dataNum);
	for(i=0; i<pMyTest->dataNum;++i) {
		if(i % 4 == 0)
			pMyTest->input[i] = (1 << (i%30)) + 1;
		else
			pMyTest->input[i] = (7 << (i%16)) + 1;
	}
	pMyTest->index = (int *)calloc(1, sizeof(int));

	pMyTest->threadNum = threadNum;
	myTest * inMyTest = (myTest *)malloc(sizeof(myTest)*threadNum);
	for(i=0; i<threadNum; ++i) {
		memcpy(inMyTest+i, pMyTest, sizeof(myTest));
		(inMyTest+i)->threadID = i;
	}
	pthread_t * tid = (pthread_t*)malloc(sizeof(pthread_t)*threadNum);
	printf("Begin create pthread.\n");
	for(i=0; i<threadNum; ++i) {
		ret = pthread_create(tid+i, NULL, (void *)thread, (myTest *)(inMyTest+i));
		if(ret != 0) {
			printf("Create pthread error.\n");
			return 0;
		}
	}
	for(i=0; i<threadNum; i++)
		pthread_join(tid[i], NULL);
	free(tid);
	free(inMyTest);
	free(pMyTest->input);
	free(pMyTest->output);
	free(pMyTest->index);
	free(pMyTest);
	return 0;
}

与上篇文章的代码作比较,不同的地方主要在于thread函数中。在这个例子中所需要判断是素数还是合数的数有100个,如果使用动态任务划分,则只有打印出结果我们才能知道哪个线程操作了整型数组中的哪个数。而使用静态任务划分的这个程序规定了线程0处理的下标为0-49,线程1处理的下标为50-99。这就是动态任务划分和静态任务划分的区别。

它们有各自的优缺点。

动态任务划分——

优点:任务结束时间均等

缺点:需要上锁和解锁,有时需要频繁等待

静态任务划分——

优点:任务划分简单,线程之间不需要通信

缺点:有时任务完成时间差距较大。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值