When to disconnect or join the accelerator thread?

I have a method that runs once every 30 seconds aprox. what i need to have in the stream.

I have a method that I can call from outside the class. Something like callThreadedMethod (), which creates a thread that itself calls the final threadedMethod.

These are MyClass methods

void callThreadedMethod(){ mThread = boost::shared_ptr<boost::thread>(new boost::thread(&MyClass::threadedMethod, this)); } void threadedMethod(){ //more code NOT inside a while loop } 

So do I need to disable mThread every time the method is called?

Is it enough to call call () in the MyClass destructor?

Does Lilith destroy herself at the end of threadedMethod?

+7
source share
1 answer

It depends on what you want to achieve. If you don't care when or if the calls to threadedMethod end, or they drop, then you can just detach stream as soon as you create it; each thread will be destroyed when the method completes. And you should not store the stream in a member variable.

If you don't care, you need to call join for each thread you create (so once for the thread, not once in the destructor). I suspect not.

Do you really need to create a new thread for each call? Creating threads can be expensive, so an alternative would be to use a thread pool and send each threadedMethod call to this. Then the pool could have a MyClass instance lifetime. But perhaps this overflow is for something that happens only once every 30 years.

+13
source

All Articles