Skip to content

The Qt example seems affected by race conditions #4

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

Closed
lultimouomo opened this issue Apr 13, 2015 · 4 comments
Closed

The Qt example seems affected by race conditions #4

lultimouomo opened this issue Apr 13, 2015 · 4 comments

Comments

@lultimouomo
Copy link

Hi,

I honestly just glanced the code, but the Qt example seems to invoking UI methods from the socket callbacks, which (I guess) are executed from an ASIO thread, not the main Qt one.

https://github.com/socketio/socket.io-client-cpp/blob/9663ca3c64a79c5bbd9f403fddd8a82e70f6f589/examples/QT/SioChatDemo/mainwindow.cpp#L41-43 should use Qt::QueuedConnection, otherwise the connected slots will be executed in line in the current thread.

@melode11
Copy link
Contributor

Thanks for reporting, I'm just start to use QT for this demo, what you guess about the thread is true, and the purpose I used Signals/Slots is to invoke the UI operations in UI thread. It seems I choose a wrong way? I'll try to verify.

@melode11
Copy link
Contributor

Hi, @lultimouomo I've test by set breaking point at AddListItem() (which is triggered by signal from asio thread) and OnMessageReturn()(which is triggered by return key, absolutely in UI thread)
I found AddListItem() executes in the same thread with OnMessageReturn(),
which means the signals/slots mechanism WILL post the invocation to the UI thread.

@lultimouomo
Copy link
Author

You're right, I thought the connection type (i.e. whether to execute the slot in line or post an event on the loop) was chosen at connection time, according to the thread affinity of the sender and the receiver.
http://doc.qt.io/qt-5/qt.html#ConnectionType-enum states instead that it's chosen when the signal is actually emitted, so the code is OK.
I would consider indicating Qt::QueuedConnection explicitely anyway, since it makes the code intention more clear.

@melode11
Copy link
Contributor

Hi, @lultimouomo I think indicating Qt::QueuedConnection explicitly is not the good choice, AutoConnection is better than QueuedConnection in this case because If you did signal AddListItem in UI thread somewhere, inline execution is preferred, so you can indicating Qt::AutoConnection explicitly instead.

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

No branches or pull requests

2 participants