views:

103

answers:

3

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.

+2  A: 

Take a look at TBB - Intel Threading Building Blocks.

Nikolai N Fetissov
+3  A: 

If you don't mind a Boost dependency, threadpool might fit your needs.

greyfade
It looks like threadpoool will eventually implement a deadline scheduler, but for now, the closest it has is a priority scheduler.
Karmastan
+1  A: 

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.

Karmastan
First of all thank you! Now, I think I'm a bit misunderstood - my need is for a good task-queue implementation that does it like the Earliest Deadline First algorithm you posted, in a manner that it checks only the first task in the queue, and in a manner that whena task is submitted to the engine, it's being inserted to the queue having a priority relative to other existing tasks, so for example if I post 3 tasks, the first is to be executed within 3sec, 2nd within 2sec and 3rd within 6sec, then the 2nd will be the first to be executed.
Poni
(continue) If after 1sec I pass another task (4rd) that should be executed within 4sec, it'll be executed before the 3rd task and after the 1st task (was set to be fired within 3sec).
Poni
So, it doesn't matter to me if a task will take a bit too long and make other tasks executed some time after their original redemption time (I'll handle it).
Poni