实现低优先级后台线程的模式? [英] Pattern for realizing low priority background threads?

查看:621
本文介绍了实现低优先级后台线程的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(软)实时系统查询一些传感器数据,进行一些处理,然后等待下一组传感器数据。传感器数据在接收器线程中读取并放入队列中,因此主线程通过互斥体进行睡眠,直到新数据到达。



还有其他的任务,如日志记录或者在后台做一些长期的计算。这些被实现为在其他线程中运行。



然而,重要的是,当主线程处理传感器数据时,它应该具有最高优先级,这意味着其他线程根本不应该消耗任何CPU资源可能(目前的后台线程导致主线程以不可接受的方式减慢)。



根据在使用Boost的Linux中设置线程优先级有疑问,设置线程优先级将完成此任务。我想知道如何衡量线程优先级确定的效果? (平台:Angstrom Linux,ARM PC)



有没有办法完全暂停和继续线程?



C ++中有一种模式可能意味着我自己暂停/继续吗? (我可以将后台工作分解成小块,如果允许继续工作,我可以检查每个工作部分,但问题是这些块应该是多大的)。



感谢您的想法!

解决方案

您的问题是使用OS调度程序,而不是C ++。您需要有一个真实的实时调度程序,它将在优先级较高的线程运行时阻止较低优先级的线程。



最标准 PC调度程序不是实时的。有一个用于Linux的RT调度程序 - 使用它。从阅读关于SCHED_RR和SCHED_FIFO,以及 nice 命令开始阅读。



在许多系统中,你必须生成任务(使用 fork ),以确保良好的级别和RT调度程序实际有效,您必须阅读系统手册并确定您有哪些调度模块,以及如何实施。


I have a (soft) realtime system which queries some sensor data, does some processing and then waits for the next set of sensor data. The sensor data are read in a receiver thread and put into a queue, so the main thread is "sleeping" (by means of a mutex) until the new data has arrived.

There are other tasks like logging or some long-term calculations in the background to do. These are implemented to run in other threads.

However, it is important that while the main thread processes the sensor data, it should have highest priority which means that the others threads should not consume any CPU resources at all if possible (currently the background threads cause the main thread to slow down in an unacceptable way.)

According to Setting thread priority in Linux with Boost there is doubt that setting thread priorities will do the job. I am wondering how I can measure which effect setting thread priorities really has? (Platform: Angstrom Linux, ARM PC)

Is there a way to "pause" and "continue" threads completely?

Is there a pattern in C++ to maybe realize the pause/continue on my own? (I might be able to split the background work into small chunks and I could check after every chunk of work if I am allowed to continue, but the question is how big these chunks should be etc.)

Thanks for your thoughts!

解决方案

Your problem is with OS scheduler, not the C++. You need to have a real real-time scheduler that will block lower priority threads while the higher priority thread is running.

Most "standard" PC schedulers are not real-time. There's an RT scheduler for Linux - use it. Start with reading about SCHED_RR and SCHED_FIFO, and the nice command.

In many systems, you'll have to spawn a task (using fork) to ensure the nice levels and the RT scheduler are actually effective, you have to read through the manuals of your system and figure out which scheduling modules you have and how are they implemented.

这篇关于实现低优先级后台线程的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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