gpt4 book ai didi

linux - 非抢占式线程?

转载 作者:太空狗 更新时间:2023-10-29 11:05:49 25 4
gpt4 key购买 nike

有没有一种方法可以在没有调度程序的情况下使用 pthreads,以便仅当线程明确让步或在互斥锁/cond 上被阻塞时才会发生上下文切换?如果不是,是否有一种方法可以最大限度地减少调度开销,从而尽可能少地发生强制上下文切换?

问题涉及 POSIX 线程的 Linux gcc/g++ 实现。

最佳答案

您可以使用非抢占式线程库 Pth(又名 GNU Portable Threads)。使用 --enable-pthread 配置它会创建一个插件替代 pthreads。我刚刚在我的 Mac 上构建并测试了它,它适用于简单的 pthreads 程序。

自述文件:

Pth is a very portable POSIX/ANSI-C based library for Unix platforms which provides non-preemptive priority-based scheduling for multiple threads of execution (aka `multithreading') inside event-driven applications. All threads run in the same address space of the server application, but each thread has its own individual program-counter, run-time stack, signal mask and errno variable.

The thread scheduling itself is done in a cooperative way, i.e., the threads are managed by a priority- and event-based non-preemptive scheduler. The intention is, that this way one can achieve better portability and run-time performance than with preemptive scheduling. The event facility allows threads to wait until various types of events occur, including pending I/O on filedescriptors, asynchronous signals, elapsed timers, pending I/O on message ports, thread and process termination, and even customized callback functions.

Additionally Pth provides an optional emulation API for POSIX.1c threads (`Pthreads') which can be used for backward compatibility to existing multithreaded applications.

关于linux - 非抢占式线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7529128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com