开发者

How to interrupt select/pselect running in QThread

I have a QThread that reads from a socket and sends a signal (QT signal) when there are any data available. This would be easy with blocking read(2), but i need to be able to stop the thread from outside without waiting for too long.

If I were using pthread I would use pselect and pthread_kill(thread_id, some_signal), but QThread doesn't seem to have any similar methods. And adding a dependcy on pthread to this project doesn't seem to elegant.

I also don't want to use the other ugly methods like constantly trying to read from the socket with some relatively small timeout.

Edit: The sockets are not TCP, but blueto开发者_JS百科oth L2CAP.


A not too elegant, but simple and working solution: Create a pipe and let select wait for either the pipe or my socket. This way I can stop the wait anytime by writing something to the pipe.


Instead of dealing with the threading yourself you can use the asynchronous interface of QTcpSocket. Check out the Fortune Client example.


You can send a signal to the terminate() slot of your QThread. This will stop your thread according to OS scheduling policies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜