Fwd: boost::thread and corresponding stack array

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

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.
Consider using thread_group: http://www.boost.org/doc/libs/1_43_0/doc/html/thread/thread_management.html#...

Aditya Arora
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);
That works, but is unwieldy due to the "new" and the need to call delete. Thankfully it's not the only way. With the move support you can write the following: boost::thread thr[SIZE]; for (int i =0 ; i < SIZE: ++i) thr[i] = boost::thread(functor); The thread objects are created without a thread, then you create a new thread object with a new thread and transfer that to the appropriate element of the array. You will still need to join your threads individually though as the destructor calls detach(). Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
participants (3)
-
Aditya Arora
-
Anthony Williams
-
Igor R