Boost logo

Boost Users :

From: David Klein (dave_chp_at_[hidden])
Date: 2006-10-02 06:31:17


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 <boost/thread/thread.hpp>
> #include <boost/bind.hpp>
> #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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net