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 10:06:28


On 07/26/2010 03:00 PM, Internet Retard wrote:
> I have a new 2 core CPU and I've been experimenting with boost::threads
> by attempting to keep both cores busy independent of each other.

using multiple threads in your application it not assure you that both
your cores will be used, most likely yes but not 100% sure, unless:
1) The threads do not interfere each other
2) You did a CPU affinity for each your thread.

> Say I have a queue of 4 tasks to do. Each task will be handled by one
> thread. Some tasks will take longer to complete than others so I can't
> use thread_group with 2 threads at a time and join_all() as one core may
> finish before the other and set idle until the second thread completes.
> I use boost::thread::hardware_concurrency() to determine how many CPU
> cores I have available. My problem is *knowing* when a thread/CPU core
> finishes a task and is available for another task. Here is some sample
> code. I commented what I think would be an approach. Any suggestions or
> hints?

what you can do is the following pattern:

1) Insert your jobs in a thread safe container (or do a wrapper around
    an std one).
2) Launch *all* your threads giving them a reference to the above
    container.
3) Then each thread have to do in the main loop (pseudo code here):
      Job* job = NULL;
      while( job = container.extract_job() ) {
         work on job;
      }

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