www.pudn.com > pthread_examples.rar > main.c


 
#include  
#include  
#include  
#include  
#include "pthinclude.h" 
 
#include "queue.h" 
#include "control.h" 
#include  
 
#define WFREE(p) if(NULL != p){free(p); p = NULL;} 
 
void* pthRecvSignoFun(int iSigNo, siginfo_t* pthSigInfo, void* arg) 
{	 
	printf("now I receive a signal and begin to kill pthread!\n"); 
	if(SIGUSR1 == iSigNo) 
	{ 
		pthread_kill(pthPushQueueProc, SIGQUIT); 
		pthread_kill(pthDealQueueProc, SIGQUIT); 
		pthread_kill(pthSendCtrolProc, SIGQUIT); 
		printf("SIGUSR1 == iSigNo!\n"); 
	} 
	else 
	{ 
		while(1) 
		{ 
			printf("doing nothing!\n"); 
		} 
	} 
	printf("pthread is killed , but i am living!\n"); 
	return ; 
} 
 
void* pthPushQueueFun(void *arg) 
{ 
	queue *pthPushQueue = NULL; 
	pthread_t pthId     = 0; 
	pthPushQueue  = (queue *)arg; 
	static int iCount = 0; 
	char cBuffer[64] = {0}; 
 
	node pthPushNode; 
	pthId               = pthread_self(); 
	printf("pthPushQueueFun : pthId = %d------------------\n", pthId); 
	 
	pthread_mutex_lock(&pthDataCtrol.mutex); 
	while(1) 
	{ 
		memset(&pthPushNode, 0, sizeof(node)); 
		printf("now, I am waiting for condition!\n"); 
		pthread_cond_wait(&pthDataCtrol.cond, &pthDataCtrol.mutex); 
		printf("now, I am geting condition!\n"); 
		pthread_mutex_unlock(&pthDataCtrol.mutex); 
		if(1 == pthDataCtrol.active) 
		{ 
			/*只有一个线程访问iCount*/ 
			iCount++; 
			sprintf(cBuffer, "join node %d", iCount);	 
			memcpy(pthPushNode.cNodeStr, cBuffer, sizeof(cBuffer)); 
			queue_put(pthPushQueue, &pthPushNode); 
		} 
		pthread_mutex_lock(&pthDataCtrol.mutex); 
	} 
	return; 
} 
 
void* pthDealQueueFun(void *arg) 
{ 
	queue *ppthQueue    = NULL; 
	pthread_t pthId     = 0; 
	static int i        = 0; 
	node *stPthDealNode = NULL; 
	 
	pthId               = pthread_self(); 
	printf("pthDealQueueFun : pthId = %d------------------\n", pthId); 
	ppthQueue           = (queue *)arg; 
	 
	while(1) 
	{ 
		stPthDealNode = queue_get(ppthQueue); 
		printf("Deal queue : %s\n", stPthDealNode->cNodeStr); 
	} 
 
	return ; 
} 
 
void* pthSendCtrolFun(void* arg) 
{ 
	data_control* stDataControl = NULL; 
	static int iSendCtrlCount   = 0; 
	pthread_t pthId     = 0; 
 
	stDataControl = (data_control*)arg; 
	pthId               = pthread_self(); 
	printf("pthSendCtrolFun : pthId = %d------------------\n", pthId); 
	 
 
	while(1) 
	{ 
		if(0 == (iSendCtrlCount % 2)) 
		{ 
			/*如果是偶数,则发送条件变量成立信号*/ 
			control_activate(stDataControl); 
			printf("now, cond = 1 ! \n"); 
		} 
		else 
		{ 
			/*如果是奇数,则发送条件变量不成立信号*/ 
			control_deactivate(stDataControl); 
			printf("now, cond = 0 ! \n"); 
		} 
		iSendCtrlCount++; 
		sleep(1); 
	} 
	 
	return; 
} 
 
void initialize(void) 
{ 
	/*初始化信号*/ 
	memset(&pthSigAct, 0, sizeof(struct sigaction)); 
    /*初始化队列*/ 
	queue_init(); 
	control_init(&pthDataCtrol); 
 
	/*在初始化的时候安装好信号处理函数,等待信号的触发*/ 
	sigemptyset(&pthSigAct.sa_mask); 
	sigaddset(&pthSigAct.sa_mask, SIGCHLD); 
	pthSigAct.sa_handler = pthRecvSignoFun; 
	pthSigAct.sa_flags    = SA_SIGINFO; 
	sigprocmask(SIG_SETMASK, &pthSigAct.sa_mask, NULL); 
	 
	sigaddset(&pthSigAct.sa_mask, SIGUSR1); 
	 
	if(sigaction(SIGCHLD, &pthSigAct, NULL) < 0) 
	{ 
		printf("initialize sigaction error!\n"); 
		return; 
	}		 
		 
	return ; 
} 
 
void deinitialize(void) 
{ 
	/*清理队列函数*/ 
	queue_deinit(); 
	control_destroy(&pthDataCtrol); 
} 
 
main() 
{ 
	int i    = 0; 
	int iRet = 0; 
	pthread_t pthId     = 0; 
	pthId               = pthread_self(); 
	printf("main : pthId = %d------------------\n", pthId); 
	 
    initialize(); 
	 
	//pthread_create(&pthRecvSignoProc, NULL, pthRecvSignoFun, (void*)&pthSigAct); 
	//printf("create pthRecvSignoProc successed!\n"); 
	 
	pthread_create(&pthPushQueueProc, NULL, pthPushQueueFun, (void*)&pQueue); 
	printf("create pthPushQueueProc successed!\n"); 
	 
	pthread_create(&pthDealQueueProc, NULL, pthDealQueueFun, (void*)&pQueue); 
	printf("create pthDealQueueProc successed!\n"); 
	 
	pthread_create(&pthSendCtrolProc, NULL, pthSendCtrolFun, (void*)&pthDataCtrol); 
	printf("create pthSendCtrolProc successed!\n");	 
	 
	pthread_join(pthPushQueueProc, NULL); 
	printf("join pthPushQueueProc successed!-----------------------------\n"); 
	 
	pthread_join(pthDealQueueProc, NULL); 
	printf("join pthDealQueueProc successed!-----------------------------\n"); 
	 
	pthread_join(pthSendCtrolProc, NULL); 
	printf("join pthSendCtrolProc successed!-----------------------------\n"); 
 
	while(1) 
	{ 
		printf("main function : I am not busy!\n"); 
		sleep(1); 
	} 
	 
	deinitialize(); 
}