openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.94k stars 2.55k forks source link

oscpack / udpSocket: invert the "break_" semaphore #7963

Closed artificiel closed 4 months ago

artificiel commented 4 months ago

In the oscpack udpSocket listener thread there is a flag named break_ which breaks the endless loop, resolving into the thread exiting "properly". because of the threading approach of ofxOscReceiver being changed from detach() tojoin(), a new race condition emerges: in the close() of the ofxOscSender the asynchronousBreak() function is called, which sets the flag to true in order to break the listener, but there are cases where the listener thread has not spun up yet, but will be forced to by the join(), and the first thing it does in Run() is to reset break_ to false, this removing the flag and creating the hang (the Run() will never return).

this changes inverses the logic: the break_ is initialized to false, and changed back to false at the end of Run(). this way if it's changed to true before the thread start, the thread immediately stops. resetting the flag to false makes the object reusable (even though the ofxOscReceiver pattern will re-create a new object when re-started())

(note: the volatile bool is also upgraded to std::atomic<bool>)

this solves the INSTANTLY_OUT_OF_SCOPE test case documented in #7949

ofTheo commented 4 months ago

Thanks @artificiel !