1.常用方法
设置socket非阻塞,之后使用select等设置超时时间
2.使用alarm信号量
需要注意:线程信号量掩码是线程私有的,当指定进程递交信号量时,操作系统会将信号量递交至该进程中未屏蔽该信号量的所有线程中的随机之一。
见 man 7 signal:
A signal may be generated (and thus pending) for a process as a whole
(e.g., when sent using kill(2)) or for a specific thread (e.g., certain
signals, such as SIGSEGV and SIGFPE, generated as a consequence of exe-
cuting a specific machine-language instruction are thread directed, as
are signals targeted at a specific thread using pthread_kill(3)). A
process-directed signal may be delivered to any one of the threads that
does not currently have the signal blocked. If more than one of the
threads has the signal unblocked, then the kernel chooses an arbitrary
thread to which to deliver the signal.
皆抄袭自《TCP高效编程》