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

2014-11-24 01:41:25 · 作者: · 浏览: 2

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

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

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

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


[cpp] #include
#include

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 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; idataNum;++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 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 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 pthread_join(tid[i], NULL);
free(tid);
free(inMyTest);
free(pMyTest->input);
free(pMyTest->output);
free(pMyTest->index);
free(pMyTest);
return 0;
}

#include
#include

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 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] =