Priority task queues using Boost.Asio and Boost.Thread

Suppose I have this abstract class Task like so: struct Task { virtual int doStuff() = 0; }; I then also have this: class CompositeTask : public Task { std::vector<Task> tasks; public: int doStuff() { for (std::vector<Task>::const_iterator it = tasks.begin(); it != tasks.end(); ++it) { it->do_stuff(); } } }; Now suppose that there is a point where I have a list of Tasks and I want to have them executed concurrently. To do so, I've leveraged Boost.Asio to create a thread pool task queue construct (via boost::asio::io_service). The next step beyond that is that I want to evaluate each of the tasks in CompositeTasks in parallel. I have a few questions about this: * One idea is to make a separate task thread for each CompositeTask. I'm under the impression that there is an absolute limit to the number of threads that I can make, which is determined by boost::thread::hardware_concurrency(). Is this true? If so, if the main Task queue has that many threads, would making a second task queue fail because it can't get more? * If I keep to one task queue (ie. throw all the Tasks in the CompositeTask to the main Task queue), then the possiblity exists of some kind of starvation (eg. the queue is full of CompositeTasks, and they are all blocking due to dependencies on Tasks in the queue). Is there a way to prioritize Tasks in the queue so that this doesn't happen?

* One idea is to make a separate task thread for each CompositeTask. I'm under the impression that there is an absolute limit to the number of threads that I can make, which is determined by boost::thread::hardware_concurrency(). Is this true? If so, if the main
No, the "absolute limit" depends on your OS. You can create enormous count of threads even on a one-core CPU, but if the number of threads is greater than hardware_concurrency, some of them will always be sharing the same cores, so you'll hardly gain any performance advantages.
* If I keep to one task queue (ie. throw all the Tasks in the CompositeTask to the main Task queue), then the possiblity exists of some kind of starvation (eg. the queue is full of CompositeTasks, and they are all blocking due to dependencies on Tasks in the queue). Is there a way to prioritize Tasks in the queue so that this doesn't happen?
Take a look at the following asio example: http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/example/invocation/...

Le 08/12/11 17:30, Kelvin Chung a écrit :
Now suppose that there is a point where I have a list of Tasks and I want to have them executed concurrently. To do so, I've leveraged Boost.Asio to create a thread pool task queue construct (via boost::asio::io_service). The next step beyond that is that I want to evaluate each of the tasks in CompositeTasks in parallel. I have a few questions about this:
* One idea is to make a separate task thread for each CompositeTask. I'm under the impression that there is an absolute limit to the number of threads that I can make, which is determined by boost::thread::hardware_concurrency(). Is this true?
If you want real parallelism the answer is yes. If you want concurrent threads the number is limited by the memory you have.
If so, if the main Task queue has that many threads, would making a second task queue fail because it can't get more? * If I keep to one task queue (ie. throw all the Tasks in the CompositeTask to the main Task queue), then the possiblity exists of some kind of starvation (eg. the queue is full of CompositeTasks, and they are all blocking due to dependencies on Tasks in the queue). Is there a way to prioritize Tasks in the queue so that this doesn't happen?
Boost.Task (which is not yet in Boost) try to answer the questions you make (http://gitorious.org/boost-dev/boost-dev/trees/task-dev/boost). Best, Vicente
participants (3)
-
Igor R
-
Kelvin Chung
-
Vicente J. Botet Escriba