views:

2470

answers:

7

I have data that needs to be executed on a certain background thread. I have code coming from all other threads that need to call into this. does anyone have a good tutorial or best practice on having a queue for synchronization to support this threading requirement

+1  A: 

This is an interesting article on ThreadPools:

http://www.codeproject.com/KB/threads/smartthreadpool.aspx

For simpler use cases you could also use .Net's own ThreadPool class.

0xA3
A: 

One of my favorite solutions to this problem is similar to the producer/consumer pattern.

I create a master thread (pretty much my programs Main()) which holds a blocking queue object.

This master thread spins off several worker threads which simple pop things off the central blocking thread and process them. Since it's a threadsafe blocking queue, the synchronization bits are easy--the TaskQueue.Dequeue() call will block until a task is enqueued by the producer/main thread.

You can dynamically manage the number of workers you want or fix it according to a configuration variable--since they're all just popping things off the queue, the number of workers doesn't add any complexity.

In my case, I have a service which processes several different types of "tasks". I have the queue typed to handle something generic like "TaskQueueTask". Then I subclass that and override the "Execute()" method.


I've also tried the .NET threadpool approach where in you can throw things into the pool very easily. It was extremely simple to use but also provided little control, and no guarantee of execution order, timing, etc. It's recommended only for light-weight tasks.

Michael Haren
Here's a blocking queue: http://www.eggheadcafe.com/articles/20060414.asp
Michael Haren
+1  A: 

You could either:

VonC
+6  A: 

Check out Threading in C#, by Joseph Albahari, very complete reference about multithreading. In particular, he covers producer/consumer queues.

Mauricio Scheffer
A: 

I posted an example of using a blocking queue at http://untweaked.wordpress.com/2009/01/07/blockingqueue

A: 

You can try this solution. It shows you how to implement the producer-consumer pattern. Has also some explanation on what can be done with it. Like different combinations of the number of producers and consumers.

http://devpinoy.org/blogs/jakelite/archive/2009/01/12/threading-patterns-the-producer-consumer-pattern.aspx

http://devpinoy.org/blogs/jakelite/archive/2009/02/03/threading-patterns-producer-consumer-pattern-examples.aspx

jake.stateresa
A: 

Here is an excellent example of a produce /consumer model http://www.softwareinteractions.com/blog/2009/12/8/intraprocess-message-queue.html

Laureano