Boost logo

Boost Users :

Subject: Re: [Boost-users] Using boost::thread to keep all cores busy
From: Gaetano Mendola (mendola_at_[hidden])
Date: 2010-07-26 12:50:10


On 07/26/2010 05:38 PM, Internet Retard wrote:
>
> Perhaps something like this then:
>
> while ( !jobs.empty() )
> {
> if ( thread_count < cores )
> {
> boost::thread t(boost::bind(&worker, jobs.front()));
> jobs.pop();
> t.join();
> }
> }

this approach is incorrect, look what can happen if for example
the jobs container has last job to do:

thread A performs the check jobs.empty() and it pass, before the
thread A executes the job.front() another thread B does the check
jobs.empty() and it pass as well, then thread A does the jobs.front()
and then when thread B does a jobs.front() as well.

that's why I suggested to do:

while( job = jobs.front() ) {
  work on job
}

then jobs.front() will have to return a null pointer, throw an
exception, whatever.

And as suggested by others, instead to use this list as
reference to learn the multi-threading programming, buy a good
book about concurrency programming. Believe me, multi threading
is very subtle.

Regards
Gaetano Mendola


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net