Producer food chain and flow planning in .net

I created a chain of four flows of consumer producers (forming a 4-stage pipeline). To my surprise, all four threads work sequentially, and not simultaneously !!! That is, the second thread mysteriously waits until the first thread is fully completed. The third thread mysteriously waits until the second thread is fully executed (and so on).

Is getting worse. If I put Thread.Sleep (300) in the loop of the first manufacturer, then the other three threads will become parallel and actually receive processor time, as expected, creating the output of "random alternating" consoles, as expected, from a multi-threaded application. I can hardly accept the idea that “sleep” is a necessary part of the solution, and yet I see that sleep is included in this way in the code written by John Skeet .

Please tell me that there is no need to achieve concurrency, or if so, why ?

A more accurate story about my specific chain of producers and consumers is as follows:

  • The first thread: in a narrow loop, it creates widget messages as quickly as possible, pushing them into the queue for the next thread. System.Threading.Timer is set to ~ 100 milliseconds when the first widget is added to the queue. The timer callback is the second thread ...
  • The second thread (started from the timer): reads some or all of the widgets from the previous queue. He sends them to another queue (for consumption by a third thread). The monitor.Pulse / Wait mechanism is used to synchronize with the third thread.
  • Third stream: Lock on the monitor. Wait until the monitor comes out. The impulse is called, and then it extracts one element from the queue. One element is placed in the final queue, again using monitor.Pulse when a push is made.
  • : . , . .

1 4 . 4 PLENTY 3 , , . , , , . .

, ?

p.s. , , , , . , ( ), , .:)

+3
2

. , , . , , .:)

/, , .

, Thread.Sleep (. , ) , /. , , .

.NET 4.0 BlockingCollection<T> . Microsoft Rx library .NET 3.5, , . ( ).

+1

Source: https://habr.com/ru/post/1760834/


All Articles