5

I'm trying to have a class that when created, starts a background thread, similar to that below:

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

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

I can't get it to compile, the error is "No matching function for call to boost::thread::thread(unresolved function type)". When I do this outside of a class it works fine. How can I get the function pointer to work?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Matthew Finlay
  • 3,354
  • 2
  • 27
  • 32

3 Answers3

6

You should initialize thread_ as:

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

Process is a member non-static method of class Test. You can either:

  • declare Process as static.
  • bind a Test instance to call Process on.

If you declare Process as static, the initializer should just be

&Test::Process

Otherwise, you can bind an instance of Test using Boost.Bind:

boost::bind(&Test::Process, this)
Pablo
  • 8,644
  • 2
  • 39
  • 29
4

The problem is that you want to initialize boost::thread with a pointer to a member function.

You would need:

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

}

Also this question may be very helpful.

Community
  • 1
  • 1
Roman Byshko
  • 8,591
  • 7
  • 35
  • 57
0

Make your Process method static:

  static void Process()
  {
   ...
  }
vitakot
  • 3,786
  • 4
  • 27
  • 59