Call a function concurrently on separate thread
I am working on a network programming and I have created a thread pool. It basically has a queue with mutex lock and condition variabl开发者_StackOverflowe and 5 child threads compete to get a work from the queue. It seems like working correctly with locking and unlocking with the condition variable.
But the problems is that if I call a function from the child thread then only one thread is allowed work on the function(fromByte). For example, if thread 1 called the function then the other thread won't be able to enter the function.
void WorkHandler::workLoop(){
printf("WorkHandler::workLoop, called\n");
while(m_workHandlerRun){
Work *work = getWork();
char *pdata = work->getMsg();
/*
* Get type only
*/
unsigned char type = pdata[0];
printf("WorkHandler::workLoop, type %d\n", type);
Packet *packet = m_packetFactory->createInstance(static_cast<PACKET_TYPES>(type));
packet->fromByte(pdata);
}
}
This is the work loop that the child threads are running and it calls the fromByte() after it gets the appropriate class instance from the factory. If I see the log statement then only one thread are allowed to work on the fromByte function and if the thread finished then other thread can work in the function. In other words, if a thread is currently in the function then other threads wait until the thread finish the work.
bool WorkHandler::initThreads(){
for(int i=0; i < m_maxThreads; i++){
pthread_t *thread(new pthread_t);
m_workThreadList.push_back(thread);
if(pthread_create(thread, NULL, runWorkThread, reinterpret_cast<void *>(this))!=0){
perror("WorkHandler::initThreads, pthread_create error \n");
return false;
}
pthread_detach(*thread);
}
return true;
}
This is how I spawn a thread and runWorkThread is a static method to call the workLoop function. How do I fix my code so that child threads can work on the function concurrently. Thanks in advance..
Edit
I am locking and unlocking like this
void WorkHandler::addWork(Work* w){
printf("WorkHandler::insertWork Thread, insertWork locking \n");
lock();
printf("WorkHandler::insertWork Locked, and inserting into queue \n");
m_workQueue.push(w);
signal();
unLock();
}
Work* WorkHandler::getWork(){
printf("WorkHandler::getWork, locking (tid : %lu) \n", pthread_self());
lock();
printf("WorkHandler::getWork, locked (tid : %lu) \n", pthread_self());
while(m_workQueue.empty()){//Need 'while' instead of 'If'
printf("WorkHandler::getWork, waiting... (tid : %lu) \n", pthread_self());
wait();
printf("WorkHandler::getWork, waiting DONE (tid : %lu) \n", pthread_self());
}
Work *work = m_workQueue.front();
printf("WorkHandler::getWork, got a job (tid : %lu) \n", pthread_self());
m_workQueue.pop();
unLock();
return work;
}
Also, this class extends MutexdCondtion class I created MutexCondition.cpp file
bool MutexCondition::init(){
printf("MutexCondition::init called\n");
pthread_mutex_init(&m_mut, NULL);
pthread_cond_init(&m_con, NULL);
return true;
}
bool MutexCondition::destroy(){
pthread_mutex_destroy(&m_mut);
pthread_cond_destroy(&m_con);
return true;
}
bool MutexCondition::lock(){
pthread_mutex_lock(&m_mut);
return true;
}
bool MutexCondition::unLock(){
pthread_mutex_unlock(&m_mut);
return true;
}
bool MutexCondition::wait(){
pthread_cond_wait(&m_con, &m_mut);
return true;
}
bool MutexCondition::signal(){
pthread_cond_signal(&m_con);
return true;
}
the problem is that if I call a function from the child thread then only one thread is allowed work
If you base this assumption on your printf, then you are wrong. It's possible that working thread finishes it's work before new item is placed in queue. This creates possibility, that same function will pick up two items in a row.
There is no way that for other threads to wait for working one, since there is nothing that blocks them after getWork() return.
精彩评论