多线程,信号!!!

来源:互联网 发布:大富豪3.4.1最新源码 编辑:程序博客网 时间:2024/05/16 00:59

http://linux.die.net/man/7/pthreads

Linux implementations of POSIX threads

Over time, two threading implementations have been provided by the GNU C library on Linux:
LinuxThreads
This is the original Pthreads implementation. Since glibc 2.4, this implementation is no longer supported.
NPTL (Native POSIX Threads Library)
This is the modern Pthreads implementation. By comparison with LinuxThreads, NPTL provides closer conformance to the requirements of the POSIX.1 specification and better performance when creating large numbers of threads.NPTL is available since glibc 2.3.2, and requires features that are present in the Linux 2.6 kernel.
Both of these are so-called 1:1 implementations, meaning that each thread maps to a kernel scheduling entity. Both threading implementations employ the Linux clone(2) system call. In NPTL, thread synchronization primitives (mutexes, thread joining, and so on) are implemented using the Linux futex(2) system call.
bash$ getconf GNU_LIBPTHREAD_VERSION

NPTL 2.3.4


http://www.ibm.com/developerworks/cn/linux/l-nptl/index.html

NPTL不再像LinuxThreads那样需要使用用户级的管理线程来维护线程的创建和销毁,LinuxThreads有一个管理线程__pthread_manager

http://www.ibm.com/developerworks/cn/linux/l-threading.html

关于编写安全的信号处理函数主要有以下一些规则:

  • 信号处理函数尽量只执行简单的操作,譬如只是设置一个外部变量,其它复杂的操作留在信号处理函数之外执行;
  • errno 是线程安全,即每个线程有自己的 errno,但不是异步信号安全。如果信号处理函数比较复杂,且调用了可能会改变 errno 值的库函数,必须考虑在信号处理函数开始时保存、结束的时候恢复被中断线程的 errno 值;
  • 信号处理函数只能调用可以重入的 C 库函数;譬如不能调用 malloc(),free()以及标准 I/O 库函数等;
  • 信号处理函数如果需要访问全局变量,在定义此全局变量时须将其声明为 volatile,以避免编译器不恰当的优化。
  • 由于 NPTL 是 POSIX 兼容的,因此它对信号的处理是按照每进程的原则进行的;getpid() 会为所有的线程返回相同的进程 ID。例如,如果发送了 SIGSTOP 信号,那么整个进程都会停止;使用 LinuxThreads,只有接收到这个信号的线程才会停止。这样可以在基于 NPTL 的应用程序上更好地利用调试器,例如 GDB。


0 0
原创粉丝点击