pthread function from a class

前端 未结 9 1063
天命终不由人
天命终不由人 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:41

    C++ : How to pass class member function to pthread_create()?

    http://thispointer.com/c-how-to-pass-class-member-function-to-pthread_create/

    typedef void * (*THREADFUNCPTR)(void *);
    
    class C { 
       // ...
       void *print(void *) { cout << "Hello"; }
    }
    
    pthread_create(&threadId, NULL, (THREADFUNCPTR) &C::print, NULL);
    
    0 讨论(0)
  • 2020-11-22 01:43

    The above answers are good, but in my case, 1st approach that converts the function to be a static didn't work. I was trying to convert exiting code to move into thread function but that code had lots to references to non-static class members already. The second solution of encapsulating into C++ object works, but has 3-level wrappers to run a thread.

    I had an alternate solution that uses existing C++ construct - 'friend' function, and it worked perfect for my case. An example of how I used 'friend' (will use the above same example for names showing how it can be converted into a compact form using friend)

        class MyThreadClass
        {
        public:
           MyThreadClass() {/* empty */}
           virtual ~MyThreadClass() {/* empty */}
    
           bool Init()
           {
              return (pthread_create(&_thread, NULL, &ThreadEntryFunc, this) == 0);
           }
    
           /** Will not return until the internal thread has exited. */
           void WaitForThreadToExit()
           {
              (void) pthread_join(_thread, NULL);
           }
    
        private:
           //our friend function that runs the thread task
           friend void* ThreadEntryFunc(void *);
    
           pthread_t _thread;
        };
    
        //friend is defined outside of class and without any qualifiers
        void* ThreadEntryFunc(void *obj_param) {
        MyThreadClass *thr  = ((MyThreadClass *)obj_param); 
    
        //access all the members using thr->
    
        return NULL;
        }
    

    Ofcourse, we can use boost::thread and avoid all these, but I was trying to modify the C++ code to not use boost (the code was linking against boost just for this purpose)

    0 讨论(0)
  • 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 <thread>
    

    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.

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