问题描述
我有一个线程被读取事件从输入设备以阻塞的方式,现在,当我退出我要正确地清理线程应用程序的后台运行,但我不能只是运行在pthread_join()因为线程将永远不会退出,由于阻塞IO。
I have a thread running in the background that is reading events from an input device in a blocking fashion, now when I exit the application I want to clean up the thread properly, but I can't just run a pthread_join() because the thread would never exit due to the blocking IO.
我如何妥善解决这种情况?我应该发送一个pthread_kill(theard,SIGIO)或pthread_kill(theard,SIGALRM)打破块?或者是,即使是正确的信号吗?还是有另一种方式来解决这种情况,让那个孩子线程退出阻塞读?
How do I properly solve that situation? Should I send a pthread_kill(theard, SIGIO) or a pthread_kill(theard, SIGALRM) to break the block? Is either of that even the right signal? Or is there another way to solve this situation and let that child thread exit the blocking read?
因为没有我的谷歌搜索变成了一个解决方案,目前有点摸不着头脑。
Currently a bit puzzled since none of my googling turned up a solution.
这是在Linux上使用pthreads的。
This is on Linux and using pthreads.
编辑:我打得四处SIGIO和SIGALRM,有点当我不装他们打破了阻塞的IO了信号处理程序,但在控制台上给出的消息(I / O可能),但是当我安装信号处理程序,以避免该消息,他们不再打破阻塞的IO,因此线程不会终止。所以我有种回到步骤之一。
I played around a bit with SIGIO and SIGALRM, when I don't install a signal handler they break the blocking IO up, but give a message on the console ("I/O possible") but when I install a signal handler, to avoid that message, they no longer break the blocking IO, so the thread doesn't terminate. So I am kind of back to step one.
推荐答案
老问题,这很可能得到一个新的答案,因为事情已经演变和新技术,现在可以的更好的处理信号线程。
Old question which could very well get a new answer as things have evolved and a new technology is now available to better handle signals in threads.
由于Linux内核2.6.22,该系统提供了一个所谓的新功能 signalfd()
可用于打开一个文件描述了一组给定的Unix信号(那些彻底杀死一个过程之外。)
Since Linux kernel 2.6.22, the system offers a new function called signalfd()
which can be used to open a file descriptor for a given set of Unix signals (outside of those that outright kill a process.)
// defined a set of signals
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
// ... you can add more than one ...
// prevent the default signal behavior (very important)
sigprocmask(SIG_BLOCK, &set, nullptr);
// open a file descriptor using that set of Unix signal
f_socket = signalfd(-1, &set, SFD_NONBLOCK | SFD_CLOEXEC);
现在,你可以使用调查()
或选择()
功能收听沿多信号通常的文件描述符(插槽,在磁盘上的文件等),您正在收听的。
Now you can use the poll()
or select()
functions to listen to the signal along the more usual file descriptor (socket, file on disk, etc.) you were listening on.
如果你想有一个循环,可以再次检查信号和其他文件描述符遍地NONBLOCK是很重要的(即它也是重要的在你的其他文件描述符)。
The NONBLOCK is important if you want a loop that can check signals and other file descriptors over and over again (i.e. it is also important on your other file descriptor).
我有使得与(1)的定时器工作的实施方式中,(2)的插座,(3)的管道,(4)的Unix信号,(5)常规文件。其实,任何真正的文件描述符加上定时器。
I have such an implementation that works with (1) timers, (2) sockets, (3) pipes, (4) Unix signals, (5) regular files. Actually, really any file descriptor plus timers.
的
的
您也可以通过库,比如有兴趣
You may also be interested by libraries such as libevent
这篇关于如何加入被挂在阻塞的IO线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!