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
This is an interesting article on ThreadPools:
For simpler use cases you could also use .Net's own ThreadPool class.
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.
You could either:
- implement a producer/consumer model (which use not thread-safe generic queue)
- or use in your background thread a synchronized queue, which does comment on the process of Enumerating through a collection.
Check out Threading in C#, by Joseph Albahari, very complete reference about multithreading. In particular, he covers producer/consumer queues.
I posted an example of using a blocking queue at http://untweaked.wordpress.com/2009/01/07/blockingqueue
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.
Here is an excellent example of a produce /consumer model http://www.softwareinteractions.com/blog/2009/12/8/intraprocess-message-queue.html