Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-04-24 07:48:28


Ion Gaztañaga wrote:
> Sorry if this is not the place to discuss it, ...

Where else? ;-)

> ... but if we have some
> discussion about threads, I always wondered why thread () constructor
> launches automatically a thread in constructor. This means that when
> you code an active class (I mean, I class that represent a task in
> other thread), if you want to start it on demand, you must use
> dynamic memory constructing the object in heap. I mainly work in
> embedded systems that may not have dynamic memory and I think an
> static way should be provided, since those systems do have threads. I
> would prefer an option like this:
> class work_in_other_thread
> {
> work_in_other_thread(const boost::function0<void>& func)
> : m_thread(func){}
>
> void start_work()
> { //this function does not exist in boost threads
> m_thread.start();
> }
>
> private:
> boost::thread m_thread;
> };
>
> instead of :
>
> class work_in_other_thread
> {
> work_in_other_thread(const boost::function0<void>& func)
> : mp_thread(0), m_func(func) {}
>
> void start_work()
> {
> m_thread = new boost::thread(m_func);
> }
>
> private:
> std::auto_ptr<boost::thread> mp_thread;
> boost::function0<void> m_func;
> };

Use aligned_storage<> and placement new.

> I see also dynamic memory in proxy functions in the code, but I
> think it's an implementation detail that could be optimized in
> systems when needed, storing needed data in boost::thread class if
> it is a joinable thread.

This would force you to keep the boost::thread object alive for the duration
of the thread; the function object needs to be stored somewhere.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk