Re: [boost] Threads and mutexes : assign_to error

2003-04-30 Thread Douglas Gregor
On Wednesday 30 April 2003 08:44 am, Jacques Kerner wrote:
 Hi,

 I get the following error :

 error C2664: 'void boost::function0R,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

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Re: [boost] Threads and mutexes : assign_to error

2003-04-30 Thread William E. Kempf

Jacques Kerner said:
 Hi,

 I get the following error :

 error C2664: 'void boost::function0R,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 ... (?!!)

Correct.  Functors are passed by value (i.e. they must be Copyable), and
Mutexes are Noncopyable.

 So what is the right way to use mutex and threads together? Do I have to
  declare the mutex outside of the
 functor? Why?

No, you just have to enable the functor to be copyable, as per the FAQ at
http://www.boost.org/libs/thread/doc/faq.html#question5.

However, I'm going to guess from the code snippet that you really don't
want this functor to be copied?  If that's the case, you may want to make
use of boost::ref.

CTaskManager taskManager;
boost::thread_group mainThreadGroup;
mainThreadGroup.create_thread(boost::ref(taskManager));
mainThreadGroup.join_all();

-- 
William E. Kempf


___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Re: [boost] Threads and mutexes : assign_to error

2003-04-30 Thread Jacques Kerner
Douglas Gregor wrote:

On Wednesday 30 April 2003 08:44 am, Jacques Kerner wrote:
 

Hi,

I get the following error :

error C2664: 'void boost::function0R,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

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
 

Thank you very much, it works great. I had missed the fact that 
create_thread makes a copy
of the functor. Thanks again!

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost