I'd like to use boost threads, if possible,
in an upcoming project, just for the sake of portability. The problem
is that boost threads seem a bit unwieldy when it comes to quickly
creating multiple threads. Since the thread object constructor is what
actually launches the thread, you can't simply create an array
of thread objects on the stack, since each thread is only launched when
the constructor is passed a functor. Therefore, if you want an array
of threads, you need to allocate each thread object individually on the
heap, like:
boost::thread* thr[SIZE];
for (int i = 0; i < SIZE; ++i) thr[i] = new boost::thread(functor);
Then
of course, you need to iterate through the array later and call delete
on each thread object. It's much simpler to do this with plain old
pthreads, since pthreads need to be explicitly started by calling
pthread_create, so you can create an array of pthread structs on the
stack, and then call pthread_create on each one, without having to
allocate/free objects on the heap.
It would be really nice if boost threads simply had a start() member function, or something, so that you could do something like:
boost::thread thr[SIZE];
for (int i =0 ; i < SIZE: ++i) thr[i].start(functor);
Does anyone know the design considerations that went into creating boost threads, and why the designers chose to have the constructor the only way to launch a new thread?
Aditya