Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2003-04-30 09:15:08


On Wednesday 30 April 2003 08:44 am, Jacques Kerner wrote:
> Hi,
>
> I get the following error :
>
> error C2664: 'void boost::function0<R,Allocator>::assign_to(Functor)' :
> unable to convert parameter 1 from 'const CTaskManager' to 'CTaskManager'
>
> when doing this :
>
> class CTaskManager
> {
> public:
> CTaskManager();
> ~CTaskManager();
> void operator()() {}
>
> private:
> boost::mutex m_mutex;
> }
>
> and
>
> CTaskManager taskManager;
> boost::thread_group mainThreadGroup;
> mainThreadGroup.create_thread(taskManager);
> mainThreadGroup.join_all();
>
> The error dissapears when I remove the mutex from the definition of
> CTaskManager ... (?!!)
>
> So what is the right way to use mutex and threads together? Do I have to
> declare the mutex outside of the
> functor? Why?

Mutexes are not copyable, and when you pass taskManager to create_thread it
tries to make a local copy of the function object . If you would prefer to
pass a reference to taskManager to create_thread, do this:

  mainThreadGroup.create_thread(boost::ref(taskManager));

        Doug


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