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:
- creation of thread static function means 1 copy of function.
- 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
Post a Comment