Problem #1: No more than 1 thread will ever do a job at a time. Since the m_request mutex is locked for the duration of the job, this ensures that only 1 thread can be doing a job at a time. This essentially makes this threadpool completely worthless.
You are right! i have solved in this way.
std::unique_lock<std::mutex> ul_request(m_request);
while((request.size()==0) && (closed==false)){
cv.wait(ul_request);
}
1 2 3 4 5 6 7 8 9 10 11 12
|
if(closed==true && request.size()==0) return true;
// se la coda è stata chiusa, e la lista è vuota muoio
if(closed==false && request.size()>0){
////////////////////////////////////////////////
// inizio Lavoro sulla coda
///////////////////////////////////////////////
std::string afn=request.front();
std::cout<<"hello i'am wake up for serving "<< afn<<std::endl;
request.pop();
ul_request.unlock();
|
cv.notify_all();
///////////////////////////////////////////////
// fine Lavoro sulla coda
///////////////////////////////////////////////
put_responses(Filename_with_id(afn,File_contain_string(afn))); |
Problem #2: You are not guarding access to the 'closed' member variable. Every thread polls it, but polling it during their run loop is not guarded which means there is possibility for race conditions. Remember that all shared variable accesses must be guarded. So you need to guard this not only in your thread loop, but also in your destructor where you're setting it to true. Either that, or make this variable atomic.
ok , i have to protect it with a new mutex , true?
Problem #3: You do not wait for all threads to close before you destroy the ThreadPool object. This means destructors for all your locks/mutexes/etc can occur while threads are still running, practically ensuring you get strange/broken behavior when the object is destroyed. This might also lead to deadlocks or crashes.
I have introduced a wait method that remain locked till all child dies...
can i insert it also in the destructor?
Not a problem, but something very weird: Why do you have a separate 'start' function? Why not just do that in the constructor? This means the class will be broken / in a bad state if you forget to call start.
I dont know why :) is my first multithread program ...i'm not so sure of what i'am doing :)
Thanks a lot. it is very useful for me!