boost::thread passing reference of the data object to thread func does not work

Hi,
Sorry to disturb the list but I've been stuck with this problem for a while. The
idea is to pass the reference of some data object to the thread function, in
which the data object (by which the ref is passed in) will be modified. However
I have found that this would not work at run-time, as the program compiled, ran
but the modifying code silently failed to execute.
I've written a small sample, which is self-contained and compilable:
//
// File: thread_test.cc
// Author: xli
//
// Created on December 26, 2007, 12:26 AM
//
#include

Xiaofan Li: ...
for (int i = 0; i < 10; ++i) threads.create_thread( boost::bind( boost::mem_fn( &threaded::increment_count ), &_thrd, a ) );
Use boost::bind( &threaded::increment_count, &_thrd, boost::ref( a ) ) to make boost::bind store a reference to 'a', rather than a copy.

Solved my problem precisely. -- Xiaofan Li On Fri, 2007-12-28 at 20:34 +0200, Peter Dimov wrote:
Xiaofan Li:
...
for (int i = 0; i < 10; ++i) threads.create_thread( boost::bind( boost::mem_fn( &threaded::increment_count ), &_thrd, a ) );
Use
boost::bind( &threaded::increment_count, &_thrd, boost::ref( a ) )
to make boost::bind store a reference to 'a', rather than a copy. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Peter Dimov
-
Xiaofan Li