防止Linux线程被调度程序中断 [英] prevent linux thread from being interrupted by scheduler

查看:137
本文介绍了防止Linux线程被调度程序中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何告诉Linux中的线程调度程序不因任何原因中断您的线程?我正在用户模式下编程.简单地锁定互斥锁是否可以完成此操作?我想防止执行某些功能时调度进程中的其他线程.它们会阻塞,我会用上下文切换浪费CPU周期.我希望即使超过线程的时间片,执行该函数的所有线程也能够在不中断的情况下完成执行.

How do you tell the thread scheduler in linux to not interrupt your thread for any reason? I am programming in user mode. Does simply locking a mutex acomplish this? I want to prevent other threads in my process from being scheduled when a certain function is executing. They would block and I would be wasting cpu cycles with context switches. I want any thread executing the function to be able to finish executing without interruption even if the threads' timeslice is exceeded.

推荐答案

您如何告诉Linux中的线程调度程序不因任何原因中断您的线程?

How do you tell the thread scheduler in linux to not interrupt your thread for any reason?

实际上无法完成,您需要一个实时系统.您将在linux上完成的事情是 将调度策略设置为实时调度程序,例如SCHED_FIFO,并且还设置了PTHREAD_EXPLICIT_SCHED属性.参见例如即使在现在,例如,此处. irq处理程序和其他其他东西将中断您的线程并运行.

Can't really be done, you need a real time system for that. The closes thing you'll get with linux is to set the scheduling policy to a realtime scheduler, e.g. SCHED_FIFO, and also set the PTHREAD_EXPLICIT_SCHED attribute. See e.g. here , even now though, e.g. irq handlers and other other stuff will interrupt your thread and run.

但是,如果您只关心自己进程中的线程无法执行任何操作,那么可以,将它们阻塞在您正在运行的线程所持有的互斥体上就足够了.

However, if you only care about the threads in your own process not being able to do anything, then yes, having them block on a mutex your running thread holds is sufficient.

最困难的部分是在您的线程需要执行其操作时协调所有其他线程以获取该互斥体.

The hard part is to coordinate all the other threads to grab that mutex whenever your thread needs to do its thing.

这篇关于防止Linux线程被调度程序中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆