jonnew / Oat

Real-time position tracker for behavioral research
GNU General Public License v3.0
49 stars 19 forks source link

Interactive recording: Ensure thread is joined. #18

Closed shlevy closed 8 years ago

shlevy commented 8 years ago

The destructor of std::thread will call std::terminate if the thread is still running and is joinable.

shlevy commented 8 years ago

It would probably be better to either a) detach the thread or b) wrap the thread in a class with a destructor that calls cleanup.

jonnew commented 8 years ago

So the advantage of detaching is that we would not need to call join() in the cleanup() function, right? A class would be OK, but it feels wrong to create such a specialized one for this task.

shlevy commented 8 years ago

Yeah, that's what detaching would do. But the downside is that the process won't wait for the thread to finish before exiting, so if there's any cleanup that thread needs to do it's a non-starter.

Having a class with a custom destructor is pretty idiomatic C++ here IMO. Doesn't mean it's the right way to do it, of course, but a big part of the rationale for "terminate on joinable thread destruction" is that it's so easy to get another behavior with your own wrapper.

jonnew commented 8 years ago

I see, thanks for the clarification. I suppose I will open an issue referring to this and merge this fix in the meantime.