Semaphore like scheduling of tasks

May 16, 2014 at 7:33 AM
We have a need to limit the number of tasks run that access a shared resource.

It'd be nice if there was a container (or task) that could have tasks added to it, and it would send to the scheduler tasks if the number was below the configured number.

This way we can keep creating tasks and when they go over the set number they are held onto, until tasks that have been scheduled complete.

It'd be great to then have a .then so when all the tasks are completed, a further task chain can be started.

Does a mechanism like this exist already?
May 16, 2014 at 5:21 PM
Hi Simon,

One way you could accomplish this by using a custom scheduler that controls the number of tasks allowed to access the shared resource. This can be one by providing you own scheduler instance implementing the pplx::scheduler_interface. The scheduler can then be passed to pplx::create_task and continuations (.then(...)). For an example take a look at pplx_scheduler.cpp and pplx_op_test.cpp under Release\tests\functional\pplx\pplx_test.

For the second one being able to wait on until a group of tasks have all completed we have that functionality built in. Take a look at the function pplx::when_all. Basically it takes a group of tasks and returns to you a task that completes once all of the provided tasks are done.