|
Boost Users : |
Subject: [Boost-users] [Threads] How to keep number of running thread constant, performing different operations
From: Alessandro Candini (candini_at_[hidden])
Date: 2011-06-17 03:05:59
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.
-- Alessandro Candini MEEO S.r.l. Via Saragat 9 I-44122 Ferrara, Italy Tel: +39 0532 1861501 Fax: +39 0532 1861637 http://www.meeo.it ======================================== "ATTENZIONE:le informazioni contenute in questo messaggio sono da considerarsi confidenziali ed il loro utilizzo è riservato unicamente al destinatario sopra indicato. Chi dovesse ricevere questo messaggio per errore è tenuto ad informare il mittente ed a rimuoverlo definitivamente da ogni supporto elettronico o cartaceo." "WARNING:This message contains confidential and/or proprietary information which may be subject to privilege or immunity and which is intended for use of its addressee only. Should you receive this message in error, you are kindly requested to inform the sender and to definitively remove it from any paper or electronic format."
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