c++用于计划任务的多线程任务队列
我需要开发一个模块来执行计划任务。
每个任务都计划在 X 毫秒内执行。
该模块将执行任务的工作线程数量作为参数。
任务堆积在一个队列中,该队列可能是一个优先级队列,因此线程会检查队列中的下一个任务(具有最低的“赎回”时间),因此无需每次都迭代所有任务。
有没有公共图书馆可以这样做,或者我应该自己建立图书馆吗?
注意:我在 Windows 上使用 VC2008。
I need to develop a module which will execute scheduled tasks.
Each task is scheduled to be executed within X milliseconds.
The module takes as a parameter an amount of worker threads to execute the tasks.
The tasks are piled up in a queue which will probably be a priority queue, so a thread checks for the next-in-queue task (the one with the lowest "redemption" time), thus there's no need to iterate through all tasks each time.
Is there any public library that does that or shall I roll my own?
Note: I'm using VC2008 on Windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不介意 Boost 依赖性,threadpool 可能会满足您的需求。
If you don't mind a Boost dependency, threadpool might fit your needs.
查看 TBB - 英特尔线程构建模块。
Take a look at TBB - Intel Threading Building Blocks.
只是为了向您的问题添加一些信息,您要求的是一个使用 最早截止日期优先算法。另请注意,如果没有操作系统支持,您无法保证您的程序能够在您指定的 X 毫秒期限内运行。操作系统总是可能会决定在工作中途将您的任务从其 CPU 中切换出来,从而导致需要花费不可预测的长时间才能完成。
如果您的应用程序严重依赖于您为其设置的 X 毫秒内完成的任务(或发生某些事情),则您需要运行 实时操作系统,不是普通的 Windows。
Just to add a little information to your question, what you're asking for is a real-time scheduler that uses the Earliest Deadline First algorithm. Also note that without OS support, you can't guarantee that your program will work in that X millisecond deadline you assign it. The OS could always decide to swap your task off its CPU in the middle of the job, making it take an unpredictably-long time to complete.
If your application critically depeneds on the task being done in the X milliseconds you set for it (or something blows up), you'll need to be running a real-time operating system, not regular Windows.