多线程,信号!!!

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt是一个Python的GUI编程工具包,它提供了丰富的功能和组件来创建图形用户界面。在PyQt中,多线程信号量可以用来实现并发执行和线程间的通信。 多线程是指在一个程序中同时执行多个线程,每个线程都可以独立执行不同的任务。在PyQt中,可以使用QThread类来创建和管理多线程。通过继承QThread类,并重写其run()方法,可以实现自定义的线程逻辑。在多线程中,需要注意线程间的数据共享和同步问题,可以使用信号量来进行线程间的通信和同步。 信号量是一种用于控制并发访问资源的机制。在PyQt中,可以使用QSemaphore类来创建和管理信号量。通过调用QSemaphore的acquire()方法可以获取一个信号量,如果当前信号量的值大于0,则减少信号量的值并继续执行;如果当前信号量的值为0,则阻塞线程直到有可用的信号量。通过调用QSemaphore的release()方法可以释放一个信号量,增加信号量的值。 下面是一个简单的示例代码,演示了如何在PyQt中使用多线程信号量: ```python from PyQt5.QtCore import QThread, QSemaphore class WorkerThread(QThread): def __init__(self, semaphore): super().__init__() self.semaphore = semaphore def run(self): self.semaphore.acquire() # 获取信号量 # 执行线程逻辑 print("Thread started") # 释放信号量 self.semaphore.release() # 创建一个信号量,初始值为1 semaphore = QSemaphore(1) # 创建并启动线程 thread = WorkerThread(semaphore) thread.start() # 等待线程执行完毕 thread.wait() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值