Boost :: thread inside a class

I am trying to create a class that, when created, launches a background thread similar to the one below:

class Test
{
  boost::thread thread_;
  void Process()
  {
    ...
  }

  public:
    Test()
    {
       thread_ = boost::thread(Process);
    }
}

I can not compile it, error: "There is no corresponding function to call for boost :: thread :: thread (unresolved function type)." When I do this outside the class, it works great. How can I make a function pointer work?

+5
source share
3 answers

You should initialize thread_as:

Test()
  : thread_( <initialization here, see below> )
{
}

Processis a member of a non-static class method Test. You can:

  • declare Processas static.
  • bind a test instance to call Processon.

Process ,

&Test::Process

Test Boost.Bind:

boost::bind(&Test::Process, this)
+6

, boost:: thread -.

:

Test()
    :thread_(boost::bind(&Test::Process, this));
{

}

question .

+4

Make your process method static:

  static void Process()
  {
   ...
  }
0
source

All Articles