1

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.

Poni
  • 11,061
  • 25
  • 80
  • 121
  • Couple of years back, I was implemented a thread pool library (very simplistic); and open sourced it. http://shobhitgupta12.blogspot.com/2008/03/thread-pool-implementation-using-c.html It was a personal project, so I didn't do very good documentation. Not an exact solution to your problem, but atleast you have something to look at now... You anyway will have to implement a thread pool mechanism, and this can help. – bits Jul 17 '10 at 22:06
  • In VC 2010 the Parallel Pattern Library, Asynchronous Agents Library and Concurrency Runtime were added explicitly to make these scenarios more productive. – Rick Jul 19 '10 at 03:12

3 Answers3

5

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

greyfade
  • 24,948
  • 7
  • 64
  • 80
  • It looks like threadpoool will eventually implement a deadline scheduler, but for now, the closest it has is a priority scheduler. – Karmastan Jul 19 '10 at 00:57
3

Take a look at TBB - Intel Threading Building Blocks.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

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
  • 5,618
  • 18
  • 24
  • 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 Jul 19 '10 at 00:47
  • (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 Jul 19 '10 at 00:48
  • 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 Jul 19 '10 at 00:50