Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] stop worker thread properly on TuioClient disconnect (win only) #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ivan-Grebennikov
Copy link

I think there is one major disadvantage of TuioClient::disconnect().
It's not synchronous. So after it returns there is no guarantee that client has disconnected.
This can lead in undesirable consequences. For example, take a look at this snippet:

std::unique_ptr<TUIO::TuioClient> tuioClient = std::make_unique<TUIO::TuioClient>();
tuioClient->connect();
/// later...
tuioClient->disconnect();
tuioClient.reset()

This snippet can lead to segfault or zombie working thread because of TuioClient destructor call and internal structures cleanup while the working thread is still running.

This PR makes TuioClient::disconnect() synchronous. The main idea is that we use

void AsynchronousBreak()
to stop the thread. It raises _breakEvent in
SetEvent( breakEvent_ );
which in turn helps us to force immediate break from the working thread polling loop:
DWORD waitResult = WaitForMultipleObjects( (DWORD)socketListeners_.size() + 1, &events[0], FALSE, waitTime );

and after we wait for thread finish using WaitForSingleObject().

Using just

leads to wait forever for new data on sockets and just break after, but what if we deinitialize network in other non-worker thread? (to be concrete ~TuioClient() calls in the chain of destructors and this leads to zombie thread that will never return from
DWORD waitResult = WaitForMultipleObjects( (DWORD)socketListeners_.size() + 1, &events[0], FALSE, waitTime );
).

This edit is just for Win because I have no opportunity to test it on Unix...

Feel free to comment.
Ivan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant