How to pass data to thread

Hi,
I want to pass data to a thread function. My question is- what is wrong with this code? The compiler error output is: "error: function returning a function". In the code below I am trying to pass an int to the thread function.
// ------------- code: how to pass data to the thread ? ----------------
#include

Monica Gretzer wrote:
Hi,
I want to pass data to a thread function. My question is- what is wrong with this code? The compiler error output is: "error: function returning a function". In the code below I am trying to pass an int to the thread function.
// ------------- code: how to pass data to the thread ? ---------------- #include
#include #include <iostream> using namespace std;
class TestThread { public: void thread(int a); void CreateThreads(void); };
void TestThread::thread(int a) { cout << "I am thread " << a << endl; }
void TestThread::CreateThreads() { boost::thread t1( boost::bind( &TestThread::thread, 10)); boost::thread t2( boost::bind( &TestThread::thread, 20));
The ::thread method is non-static, and you don't pass any object to bind. You should have boost::bind (&TestThread::thread, some_object, 10) HTH, Volodya

Vladimir Prus wrote:
Monica Gretzer wrote:
void TestThread::thread(int a) { cout << "I am thread " << a << endl; }
void TestThread::CreateThreads() { boost::thread t1( boost::bind( &TestThread::thread, 10)); boost::thread t2( boost::bind( &TestThread::thread, 20));
The ::thread method is non-static, and you don't pass any object to bind. You should have
boost::bind (&TestThread::thread, some_object, 10)
... or, in this case, since you probably want to call this->thread( 10 ) in a thread, you need boost::thread t1( boost::bind( &TestThread::thread, this, 10)); boost::thread t2( boost::bind( &TestThread::thread, this, 20));

Hi,
Thanks for your answers. Now I wonder how to a certain number of threads which is decided at runtime and can vary between program runs. I send the number of threads as an argument to CreateThreads.
// ----------- create a number of threads that is known at runtime --------
#include
Monica Gretzer wrote:
void TestThread::thread(int a) { cout << "I am thread " << a << endl; }
void TestThread::CreateThreads() { boost::thread t1( boost::bind( &TestThread::thread, 10)); boost::thread t2( boost::bind( &TestThread::thread, 20));
The ::thread method is non-static, and you don't pass any object to bind. You should have
boost::bind (&TestThread::thread, some_object, 10)
... or, in this case, since you probably want to call this->thread( 10 ) in a thread, you need boost::thread t1( boost::bind( &TestThread::thread, this, 10)); boost::thread t2( boost::bind( &TestThread::thread, this, 20)); _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Monica Gretzer wrote:
Hi,
Thanks for your answers. Now I wonder how to a certain number of threads which is decided at runtime and can vary between program runs. I send the number of threads as an argument to CreateThreads.
[...]
void TestThread::CreateThreads(int nb) { boost::thread t1( boost::bind( &TestThread::thread, this, 1)); boost::thread t2( boost::bind( &TestThread::thread, this, 2)); boost::thread t3( boost::bind( &TestThread::threas, this, 3));
t1.join(); t2.join(); t3.join(); }
If you really like to wait for the threads to finish (which is what you do with tN.join), you need to use a thread_group: http://www.boost.org/doc/html/thread_group.html void TestThread::CreateThreads(int nb) { boost::thread_group tg; for( int i = 0; i < nb; ++i ) { tg.create_thread( boost::bind( &TestThread::thread, this, i ) ); } tg.join_all(); }

Monica Gretzer wrote:
Hi,
I want to pass data to a thread function. My question is- what is wrong with this code? The compiler error output is: "error: function returning a function". In the code below I am trying to pass an int to the thread function.
// ------------- code: how to pass data to the thread ? ---------------- #include
#include #include <iostream> using namespace std;
class TestThread { public: void thread(int a); void CreateThreads(void); };
void TestThread::thread(int a) { cout << "I am thread " << a << endl; }
void TestThread::CreateThreads() { boost::thread t1( boost::bind( &TestThread::thread, 10)); boost::thread t2( boost::bind( &TestThread::thread, 20));
// wait for the thread to finish t1.join(); t2.join(); }
int main() { TestThread t; t.CreateThreads(); } hi monica,
either make TestThread::thread static ... class TestThread { ... static void thread(int a); ... }; or use an instance of TestThread to bind to... void TestThread::CreateThreads() { TestThread tt; boost::thread t1( boost::bind( &TestThread::thread, tt, 10)); ... } -- HTH dave
participants (4)
-
David Klein
-
Monica Gretzer
-
Peter Dimov
-
Vladimir Prus