C++ master/worker

跟風遠走 提交于 2019-12-04 13:08:57

问题


I am looking for a cross-platform C++ master/worker library or work queue library. The general idea is that my application would create some sort of Task or Work objects, pass them to the work master or work queue, which would in turn execute the work in separate threads or processes. To provide a bit of context, the application is a CD ripper, and the the tasks that I want to parallelize are things like "rip track", "encode WAV to Mp3", etc.

My basic requirements are:

  • Must support a configurable number of concurrent tasks.
  • Must support dependencies between tasks, such that tasks are not executed until all tasks that they depend on have completed.
  • Must allow for cancellation of tasks (or at least not prevent me from coding cancellation into my own tasks).
  • Must allow for reporting of status and progress information back to the main application thread.
  • Must work on Windows, Mac OS X, and Linux
  • Must be open source.

It would be especially nice if this library also:

  • Integrated with Qt's signal/slot mechanism.
  • Supported the use of threads or processes for executing tasks.

By way of analogy, I'm looking for something similar to Java's ExecutorService or some other similar thread pooling library, but in cross-platform C++. Does anyone know of such a beast?

Thanks!


回答1:


I think this calls for intel's Threading Building Blocks, which pretty much does what you want.




回答2:


I haven't used it in long enough that I'm not positive whether it exactly meets your needs, but check out the Adaptive Communications Environment (ACE). This library allows you to construct "active objects" which have work queues and execute their main body in their own threads, as well as thread pools that can be shared amoung objects. Then you can pass queue work objects on to active objects for them to process. Objects can be chained in various ways. The library is fairly heavy and has a lot to it to learn, but there have been a couple of books written about it and theres a fair amount of tutorial information available online as well. It should be able to do everything you want plus more, my only concern is whether it possesses the interfaces you are looking for 'out of the box' or if you'd need to build on top of it to get exactly what you are looking for.




回答3:


Check out Intels' Thread Building Blocks library.




回答4:


Sounds like you require some kind of "Time Sharing System".

There are some good open source ones out there, but I don't know
if they have built-in QT slot support.




回答5:


This is probably a huge overkill for what you need but still worth mentioning -
BOINC is a distributed framework for such tasks. There's a main server that gives out tasks to perform and a cloud of workers that do its bidding. It is the framework behind projects like SETI@Home and many others.




回答6:


See this post for creating threads using the boost library in C++:

Simple example of threading in C++

(it is a c++ thread even though the title says c)

basically, create your own "master" object that takes a "runnable" object and starts it running in a new thread.

Then you can create new classes that implement "runnable" and throw them over to your master runner any old time you want.



来源:https://stackoverflow.com/questions/1060839/c-master-worker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!