std::thread error

前端 未结 1 1911
悲&欢浪女
悲&欢浪女 2021-02-18 20:00

I am trying to spawn a thread from within my class and the thread executes a particular method in my class. The code looks like this:

class ThreadClass{
    int          


        
1条回答
  •  失恋的感觉
    2021-02-18 20:20

    The problem is that a member function can't be called without an object. Provide a pointer to this so that the current object is used:

    thread t(&ThreadClass::myThread, this, 10);
    

    You could use an instance of any ThreadClass object, but in your case, it seems this is the right thing to do.

    NOTE: Remember you need a reference to the created thread so that you can do a join() later on.

    0 讨论(0)
提交回复
热议问题