c++ - Thread creation inside constructor -


so, using c++11 , made class

class c {   private:     queue<std::int> s;     pthread_t x;   public:     c() {phthread_create(&x, null, d_q, null);     void log(int p); // pushes q.     void* d_q(void* q); // function pop s. assume s thread safe. } 

the problem line pthread_create(&x, null, d_q, null). gives me error: reference non-static member must called.

i rid of problem making pthread_t x static. dont want because of 2 reasons:

  1. creation of thread static function means 1 copy of function.
  2. the thread created in constructor. don't know happen if create more 1 objects of class c.

can please give me workaround?

solved: help! also, advice prefer std::thread on pthread future users!

as problem, pointer (non-static) member function not same pointer member function. non-static member functions needs instance of object called on.

there couple of ways solve this: if insist on using posix thread functions make static wrapper function, pass instance argument thread, , in static wrapper function call actual function using object passed.

another solution use std::thread, make easier:

class c {     std::thread thread_;     ... public:     c() : thread_(&c::d_q, this) {}     ...     void d_q() { ... } }; 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -