Passing parameter to boost::thread no overloaded function takes 2 arguments

后端 未结 1 751
半阙折子戏
半阙折子戏 2021-01-21 20:12

From the boost::thread documentation it seems that I can pass parameters to the thread function by doing this:

boost::thread* myThread = new boost::thread(callba         


        
相关标签:
1条回答
  • 2021-01-21 20:47

    The problem is that member functions take an implicit first parameter Type*, where Type is the type of the class. This is the mechanism by which member functions get called on instances of types, and means you have to pass an extra parameter to the boost::thread constructor. You also have to pass the address of the member function as &ClassName::functionName.

    I have made a small compiling and running example that I hope illustrates the use:

    #include <boost/thread.hpp>
    #include <iostream>
    
    struct Foo
    {
      void foo(int i) 
      {
        std::cout << "foo(" << i << ")\n";
      }
      void bar()
      {
        int i = 42;
        boost::thread t(&Foo::foo, this, i);
        t.join();
      }
    };
    
    int main()
    {
      Foo f;
      f.bar();
    }
    
    0 讨论(0)
提交回复
热议问题