多线程(六):线程的多次汇合控制改进版

上篇文章的代码中sleep函数用的很是不合适,所以改了一下代码(其实就是多加了两个变量),删去sleep之后,程序瞬间结束~~

下面是代码:

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


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

	int *threadNum;
	int *input;
	int *output;
	int *index;
	int *endFlag;
	int *index2;
	int *endFlag2;
	int *repeatFlag;
	pthread_mutex_t *pMutIndex;
}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;
}

int generate(int index) {
	int input;
	if(index % 4 == 0)
		input = (1 << (index%30)) + 1;
	else
		input = (7 << (index%16)) + 1;
	return input;
}




void thread(myTest * pMyTest) {
	printf("Begin threadID=%u run!\n", pMyTest->threadID);
	int index, index2, rindex, input, output;
	int threadID = pMyTest->threadID;
	int dataNum = pMyTest->dataNum;
	int repeatNum = pMyTest->repeatNum;
	pthread_mutex_lock(pMyTest->pMutIndex);
	rindex = pMyTest->repeatFlag[0];
	pMyTest->repeatFlag[0]++;
	pthread_mutex_unlock(pMyTest->pMutIndex);
	while(rindex < pMyTest->threadNum[0]*repeatNum-1) {
		pthread_mutex_lock(pMyTest->pMutIndex);
		index = pMyTest->index[0];
		pMyTest->index[0]++;
		pthread_mutex_unlock(pMyTest->pMutIndex);
		while(index < dataNum) {
			input = pMyTest->input[index];
			output = calculate(input);
			printf("index=%3u, input=%8u, output=%2u, threadID=%2u\n", index, input, output, threadID);
			pMyTest->output[index] = output;
			pthread_mutex_lock(pMyTest->pMutIndex);
			index = pMyTest->index[0];
			pMyTest->index[0]++;
			pthread_mutex_unlock(pMyTest->pMutIndex);
			
		}
		pthread_mutex_lock(pMyTest->pMutIndex);
		pMyTest->endFlag[0]++;
		pthread_mutex_unlock(pMyTest->pMutIndex);
		pMyTest->index2[0] = 0;
		pMyTest->endFlag2[0] = 0;
		while(pMyTest->endFlag[0] < pMyTest->threadNum[0])
			;
		pthread_mutex_lock(pMyTest->pMutIndex);
		index2 = pMyTest->index2[0];
		pMyTest->input[index2] = generate(index2);
		pMyTest->output[index2] = 0;
		pMyTest->index[0]++;
		pthread_mutex_unlock(pMyTest->pMutIndex);
		while(index2 < dataNum) {
			printf("threadid=%d,rindex=%d\n", threadID, rindex);
			pthread_mutex_lock(pMyTest->pMutIndex);
			pMyTest->input[index2] = generate(index2);
			pthread_mutex_unlock(pMyTest->pMutIndex);
			index2 = pMyTest->index2[0];
			pMyTest->index2[0]++;
		}
		pthread_mutex_lock(pMyTest->pMutIndex);
		pMyTest->endFlag2[0]++;
		pthread_mutex_unlock(pMyTest->pMutIndex);
		pMyTest->index[0] = 0;
		pMyTest->endFlag[0] = 0;
		while(pMyTest->endFlag2[0] < pMyTest->threadNum[0])
			;
		pthread_mutex_lock(pMyTest->pMutIndex);
		rindex = pMyTest->repeatFlag[0];
		pMyTest->repeatFlag[0]++;
		pthread_mutex_unlock(pMyTest->pMutIndex);
		
	}
	pthread_mutex_lock(pMyTest->pMutIndex);
	pMyTest->threadNum[0]--;
	pthread_mutex_unlock(pMyTest->pMutIndex);
	pthread_exit(NULL);
}

int main(void) {
	int i, ret;
	int threadNum = 2;
	myTest * pMyTest = (myTest *)malloc(sizeof(myTest));
	pMyTest->dataNum = 30;
	pMyTest->repeatNum = 11;
	pMyTest->input = (int *)malloc(sizeof(int)*pMyTest->dataNum);
	pMyTest->output = (int *)malloc(sizeof(int)*pMyTest->dataNum);
	for(i=0; i<pMyTest->dataNum;++i) {
		pMyTest->input[i] = generate(i);
	}
	pMyTest->threadNum = (int *)calloc(1, sizeof(int));
	pMyTest->index = (int *)calloc(1, sizeof(int));
	pMyTest->endFlag = (int *)calloc(1, sizeof(int));
	pMyTest->index2 = (int *)calloc(1, sizeof(int));
	pMyTest->endFlag2 = (int *)calloc(1, sizeof(int));
	pMyTest->repeatFlag = (int *)calloc(1, sizeof(int));

	pMyTest->pMutIndex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
	pthread_mutex_init(pMyTest->pMutIndex, NULL);

	pMyTest->threadNum[0] = 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);
	pthread_mutex_destroy(pMyTest->pMutIndex);
	free(pMyTest->pMutIndex);
	free(pMyTest->threadNum);
	free(pMyTest->input);
	free(pMyTest->output);
	free(pMyTest->index);
	free(pMyTest->index2);
	free(pMyTest->endFlag);
	free(pMyTest->endFlag2);
	free(pMyTest->repeatFlag);
	free(pMyTest);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值