非抢占式 Pthreads?

发布于 12-05 21:51 字数 148 浏览 6 评论 0原文

有没有一种方法可以在没有调度程序的情况下使用pthreads,因此只有当线程显式屈服或在互斥体/条件上被阻塞时才会发生上下文切换?如果没有,是否有办法最小化调度开销,以便尽可能少地发生强制上下文切换?

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

Is there a way to use pthreads without a scheduler, so context switch occurs only if a thread explicitly yields, or is blocked on a mutex/cond? If not, is there a way to minimize the scheduling overhead, so that forced context switches will occur as rarely as possible?

The question refers to the Linux gcc/g++ implementation of POSIX threads.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁2024-12-12 21:51:28

您可以使用 Pth(又名 GNU 可移植线程),一个非抢占式线程库。使用 --enable-pthread 配置它将为 pthreads 创建插件替代品。我刚刚在我的 Mac 上构建并测试了它,对于简单的 pthreads 程序来说它工作得很好。

来自自述文件:

Pth 是一个非常可移植的基于 POSIX/ANSI-C 的 Unix 平台库
它为多个提供非抢占式基于优先级的调度
事件驱动内的执行线程(又名“多线程”)
应用程序。所有线程都运行在服务器的同一地址空间中
应用程序,但每个线程都有自己的程序计数器,
运行时堆栈、信号掩码和 errno 变量。

线程调度本身是以协作方式完成的,即
线程由基于优先级和基于事件的非抢占式管理
调度程序。目的是,通过这种方式可以取得更好的成绩
与抢占式调度相比,可移植性和运行时性能更高。
事件设施允许线程等待,直到各种类型的
事件发生,包括文件描述符上的待处理 I/O、异步
信号、已用定时器、消息端口上的待处理 I/O、线程和
进程终止,甚至自定义回调函数。

此外,Pth 还为 POSIX.1c 提供了可选的模拟 API
线程(“Pthreads”),可用于向后兼容
现有的多线程应用程序。

You can use Pth (a.k.a. GNU Portable Threads), a non-preemptive thread library. Configuring it with --enable-pthread will create a plug-in replacement for pthreads. I just built and tested this on my Mac and it works fine for a simple pthreads program.

From the README:

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.

北恋2024-12-12 21:51:28

如果您有一个进程在普通用户空间中运行,上下文切换自然会作为系统操作的一部分发生 - 总是有另一个进程需要 CPU 时间。线程之间的抢占式上下文切换已经被操作系统很好地优化了,并且有时肯定是必要的。

如果你确实遇到了过度上下文切换的问题,那么你最好首先调整 Linux 调度程序,这在这里是题外话。 pthread_setschedprio 和 pthread_setschedparam 可以设置一些提示,但仅限于设置优先级,并且这些优先级的解释是实现定义的,即由Linux调度程序决定。

If you have a process running in normal user land, context switches will naturally happen as part of the system operation - there is always another process that needs the CPU time. Preemptive context switches between your threads are quite well optimized by the OS already and are bound to be necessary sometimes.

If you really happen to have problems with excessive context switching, you are best off tweaking the Linux scheduler first, which is off-topic here. pthread_setschedprio and pthread_setschedparam can set some hints, but are limited to setting priorities, and the interpretation of these priorities is implementation-defined, i.e. up to the Linux scheduler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文