www.pudn.com > net_oss.rar > thread_pool_client.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(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);
}