38

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

leora
  • 188,729
  • 360
  • 878
  • 1,366
  • See [StackOverflow: How to work threading with ConcurrentQueue(T)](https://stackoverflow.com/q/4551087/199364) and maybe [Code Review: Simple generic multithreaded queue](https://codereview.stackexchange.com/questions/12053/simple-generic-multithreaded-queue). – ToolmakerSteve Apr 13 '18 at 18:14

5 Answers5

34

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

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
10

You could either:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The code sample in the [first link](http://msdn.microsoft.com/en-us/library/yy12yx1f.aspx) is not recommended as it has a possible race condition resulting in lost signals. Don't use it! – Hugh Jeffner Dec 17 '10 at 20:38
5

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.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
1

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

I create a master thread (pretty much my program's 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.

Sergio
  • 6,900
  • 5
  • 31
  • 55
Michael Haren
  • 105,752
  • 40
  • 168
  • 205
0

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
  • 226
  • 2
  • 3