Quantcast
Channel: User rafalcieslak - Stack Overflow
Viewing all articles
Browse latest Browse all 25

Is there a safe way to have a std::thread as a member of a class?

$
0
0

I would like to use a class that manages a thread (or several threads). Using composition, this would look like:

class MyClass{private:    std::thread mythread;    void _ThreadMain();public:    MyClass();    // other fields}

Because the default constructor for an std::thread is pointless, I would need to call it explicitly in MyClass constructor:

MyClass::MyClass() : mythread(&MyClass::_ThreadMain,this) {}

However, in this case the _ThreadMain method will be likely executed beforeMyClass is constructed, leading to any kind of weird behaviour. This is clearly unsafe. How can I fix this?

An obvious solution would be to use a pointer to std::thread instead, and add another member function:

void MyClass::Start(){    // This time mythread is of type  std::thread*    mythread = new std::thread(&MyClass::_ThreadMain,this); // One could use std::unique_pointer instead.}

which would fire up that thread. In this case it would be called after the class is constructed, which will be indeed safe.

However, I am wondering if there is any reasonable solution that would allow me not to use pointers for this. It feels like it should be possible somehow (hey, there must be a way to launch a thread when a class is constructed!), but I cannot come up with anything that would not cause troubles.

I have considered using a conditional variable so that the _ThreadMain waits till the constructor has done its work, but I cannot use one before the class is constructed, right? (This would also be unhelpful if MyClass was a derived class)


Viewing all articles
Browse latest Browse all 25

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>