Re: QThread and event loop
First question, as usual when threads are used: are you sure you need to use one?
Then a couple of suggestions:
- do not delete the socket in the detructor, delete it after exec() returns, i.e. within the worker thread.
- no need to make the QHostAddress a pointer
- do not make internal slots public
- be aware that your slots are currently executed in the main thread, so do not access "socket" in them
Cheers,
_
Re: QThread and event loop
Thanks you for your quickly response.
Yes i need to use a thread because the frequency of data's reception is important, otherwise it would block my application.
I modified my code, but there is something I do not understand...
" - be aware that your slots are currently executed in the main thread, so do not access "socket" in them " : effectively, i added
Code:
qDebug() << this->thread();
and my slot run in the main thread. But, how I can retrieve data before the error, how can I access the socket?
In the console, i can see :
Quote:
Nb byte : 936
Nb byte : 936
Nb byte : 936
Nb byte : 936
QSocketNotifier: Invalid socket 15 and type 'Read', disabling...
Nb byte : 936
Nb byte : 936
Nb byte : 936
Nb byte : 936
Sometimes this error is not blocking :confused:
Re: QThread and event loop
Quote:
Originally Posted by
Crashz83
Yes i need to use a thread because the frequency of data's reception is important, otherwise it would block my application.
Ok, I was just checking that you had implemented it without threads first and only start using threads as a last resort.
Quote:
Originally Posted by
Crashz83
I modified my code, but there is something I do not understand...
" - be aware that your slots are currently executed in the main thread, so do not access "socket" in them " : effectively, i added
Code:
qDebug() << this->thread();
and my slot run in the main thread. But, how I can retrieve data before the error, how can I access the socket?
Well, you should not access the socket from two threads. So it would make most sense to handle the signals of the socket in the same thread as the socket.
Either by having the receiver on the same thread or by forcing a direct call using Qt::DirectConnection as the connection type.
Currently you have the receiver on the main thread and are using the default connection type (Qt::AutoConnection) which results in the behavior of Qt::QueuedConnection (emitter thread and receiver owner thread different).
Cheers,
_
Re: QThread and event loop
It's ok, I simply added a call to Qt :: DirectConnexion in the connection of slots and everything works.
Thanks you very much!! :D