ParallelExtensionsExtras - how to queue tasks serially - limited to one thread at a time?





    If I want to queue tasks serially (limited to one thread from the threadpool at a time), what should I use from the ParallelExtensionsExtras lib:

    1. SerialTaskQueue

    2. Use OrderedTaskScheduler (which is LimitedConcurrencyTaskScheduler with limit of 1).


    How would you compare them?

    Would you recommend another way?




    Sunday, July 24, 2011 8:36 AM


All replies

  • Hi Shlomi,

    It's always difficult to study an answer without an usage expected. To be pragmatic, here a sumary of:

    1. SerialTaskQueue — Represents an ordered queue of tasks that has to be started and executed serially. This class allows you to queue tasks to be processed serially according to a LIFO order and to check the status of the tasks’ executions.

    2. OrderedTaskScheduler - Ensures that only one task is executing at a time. Tasks execute in the order that they were queued (FIFO). It is a subclass of LimitedConcurrencyLevelTaskScheduler that sends 1 as a parameter for its base class constructor.

    As you can see, the result depends of your needs. If the FIFO order matches your needs, the OrderedTaskScheduler class is the best choice. If not, the LIFO is mandatory, the SerialTaskQueue class is a better choice.

    Boucard Bruno - http://blogs.msdn.com/b/devpara/

    Monday, July 25, 2011 1:00 PM
  • The SerialTaskQueue in the samples is actually FIFO, not LIFO.

    Neither SerialTaskQueue or OrderedTaskScheduler is really "better", it just depends on your needs.  SerialTaskQueue was primarily meant to be an example of how you could build a simple queue like this, whereas the OrderedTaskScheduler is meant to be an example of plugging in at the lower TaskScheduler level.

    Monday, July 25, 2011 3:27 PM
  • Hi Boucard and Stephen,

    Thanks for you replies!

    If both SerialTaskQueue and OrderedTaskScheduler are FIFO - then both are viable solutions.

    I'll give you examples of what I need:

    1. I have logs manager (logs manager is one example) - which can be sometimes idle. I prefer not to designate it a worker, and instead use the threadpool when logs arrives. The logs must be written serially in the order they arrives (FIFO).

    2. I have a tcp socket server with many many client sockets connected. The server need to send frequent ordered messages to the clients. Worker per socket queue is not an option. In this case - efficiency and performance matters.


    For both solutions I wanted to use the parallel library tasks methods. For both of them I need a way to enqueue tasks which must be executed serially (ordered FIFO) per an object (i.e. one client socket, or the logs mgr) and I want to use the threadpool rather a designated workers.


    Is the above mentioned SerialTaskQueue or OrderedTaskScheduler can be good candidates for what I need?

    What would you suggest? can you think of other solutions? 


    Many thanks,



    Tuesday, July 26, 2011 11:36 AM
  • Your thoughts?


    Monday, August 01, 2011 8:22 AM
  • Hi Shlomiw-

    I'd actually suggest you check out the TPL Dataflow CTP at http://msdn.microsoft.com/en-us/devlabs/gg585582. The ActionBlock<T> type sounds like it's exactly what you want.  If you can't use the CTP and would prefer a source-based solution, the AsyncCall<T> in the samples is also likely well suited.

    Sunday, August 07, 2011 9:44 PM
  • Hi Stephen,


    Though TDF look promising - unfortunately we can't use any CTP libs on our production (you know when it's going to be released?)

    Anyway, I'll check the samples you suggested.





    p.s. I read your last msdn article: "The Past, Present and Future of Parallelizing .NET Applications" - was really good!

    Monday, August 08, 2011 9:57 AM
  • Just wanted to update:

    I didn't see the AyncCall before (hard to understand by its name that it is packed with all of these features).

    I just need to tweak it a bit to allow LongRunning tasks creation.


    Thanks a lot!



    Monday, August 08, 2011 10:25 AM