⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread_pool_client.cc

📁 这是一个模拟电信计费的程序,非常详细,运用了线程池、消息队列和socket编程等技术。
💻 CC
字号:
#include "thread_pool_client.h"using namespace std;void* processing(void* v){        #ifdef __DEBUG        cout << " # " << pthread_self() << " is serving you. " << endl;        #endif        thread_pool_client::_mesg msg;                    thread_pool_client::info inf;        thread_pool_client* cli = static_cast<thread_pool_client*>(v);        #ifdef __DEBUG        cout << " cli->mqid = " << cli->mqid << endl;        #endif        while(1)        {                memset(msg.inf,0x00,sizeof(inf));                if(msgrcv(cli->mqid,&msg,sizeof(msg),0,0)<0)                 {                				#ifdef __DEBUG                        cout << strerror(errno) << endl;                        #endif                }                else                {                	while(write(cli->serv,msg.inf,sizeof(inf))<0)                	{                		#ifdef __DEBUG                		perror("write");                		#endif                		sleep(1);                	}              	}								                    }}void thread_pool_client::start(char* IP,int port){	#ifdef __DEBUG	cout << "[start] mqid = " << mqid << endl;	#endif	strcpy(ip,IP);	serv = socket(AF_INET,SOCK_STREAM,0);	if(serv<0) 	{		#ifdef __DEBUG		ERR_REPORT();		#endif	}	sockaddr_in to;	memset(&to,0x00,sizeof(to));	to.sin_family = AF_INET;	to.sin_port = htons(port);	inet_pton(to.sin_family,ip,&to.sin_addr);		if(connect(serv,(const sockaddr*)&to,sizeof(to))<0) 	{		#ifdef __DEBUG		ERR_REPORT();		#endif	}	//create threads  pthread_t tid;  for(int i = 0; i < total; i++)  {		  #ifdef __DEBUG			cout << " creating thread " << i  << endl;			#endif	    if(pthread_create(&tid,NULL,processing,this)<0) exit(-1);  }  pthread_join(tid,NULL);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -