pthread function from a class

前端 未结 9 1061
天命终不由人
天命终不由人 2020-11-22 01:00

Let\'s say I have a class such as

class c { 
    // ...
    void *print(void *){ cout << \"Hello\"; }
}

And then I have a vector of c

9条回答
  •  爱一瞬间的悲伤
    2020-11-22 01:45

    My first answer ever in the hope that it'll be usefull to someone : I now this is an old question but I encountered exactly the same error as the above question as I'm writing a TcpServer class and I was trying to use pthreads. I found this question and I understand now why it was happening. I ended up doing this:

    #include 
    

    method to run threaded -> void* TcpServer::sockethandler(void* lp) {/*code here*/}

    and I call it with a lambda -> std::thread( [=] { sockethandler((void*)csock); } ).detach();

    that seems a clean approach to me.

提交回复
热议问题