On Fri, Jun 17, 2011 at 9:05 AM, Alessandro Candini <candini@meeo.it> wrote:
I have a problem in organizing concurrent thread launch. My program structure is the following:
#include <iostream>
#include <boost/thread.hpp>

using namespace std;

class Worker
{
   private:
       boost::thread m_Thread;

   public:
       Worker() { /* the thread is not-a-thread until we call start() */ }

       void start(int N) { m_Thread = boost::thread(&Worker::processQueue, this, N); }

       void join() { m_Thread.join(); }

       void processQueue(unsigned N) { /* Do some long long stuff... */ }
};

int main(int argc, char* argv[])
{
   Worker worker_1,
          worker_2,
          worker_3,
          worker_4;

   // How to start threads and join them in order
   // to make constantly two of them running?

   return 0;
}

I have different threads which have to work on completely different input and output data (non critical sections): an atomic operation per thread, each one with different time execution but everyone with an intense use of the CPU.
Let's say I have 10 operations to perform (10 threads): I would like to run concurrently only 2 threads because of resource consumption.

My problem is that when a threads ends its execution, I would like to suddenly start another thread performing operation 3, in order to have constantly 2 threads working, and so on until the end of operations.

How can I achieve this? I thought to insert my threads into a vector...but I have no idea on how start and join them in order to obtain what described above.

Can anyone post me a little example?

Thanks in advance.

I think you should use a thread pool. You can extend your thread pool with more threads, without impacting other application parts. Take a look at this thread pool implementation:
http://threadpool.sourceforge.net/tutorial/intro.html

 You only need to package your work in tasks and put them into the queue. After one of the threads gets ready (from the pool) it will grab another task. With this thread pool implementation you can also give task priorities.


With Kind Regards,
Ovanes