The question is theoretical, I did a test task for Veeam. The famous task about the multi-threaded archiver. In my implementation, a separate stream takes a chunk of data and compresses / decompresses it. And they sent me answers to my task. Launching a new stream to process each block is less optimal than reusing existing streams. The question is what it means to reuse the existing threads. When they already stand in the semaphore and wait for the semaphore to give them the opportunity to take a new piece of data. Synchronization primitives are not used to handle state changes (wait is implemented at sleep, which is less optimal). The question is what are the synchronization primitives for changing states.

1 answer 1

Regarding the reuse of threads already suggested in the comments, for this there is a ThreadPool .

Concerning synchronization primitives. Windows has 4 primitives for thread synchronization:

  1. Event. In .NET there is a wrapper in the form of classes AutoResetEvent / ManualResetEvent . Something similar can be done using Monitor.Pulse() and Monitor.Wait() . But Windows objects are more functional.
  2. Mutex With the same class wrapper. There is a similar functionality in .NET itself, in the methods of the Monitor class or in the C# lock operator.
  3. Semaphore. With the same class wrapper.
  4. Waitable timer. In NET, there is no wrapper for this object, but there are other timers.

In addition, threads can be synchronized across other objects, such as threads or processes. If you call thread.Join() in code, you synchronize one thread with the end of another.

  • but for what minus it? - Zergatul
  • four? Um, they are much more: Overview of synchronization primitives . - Alexander Petrov
  • @AlexanderPetrov specifically objects of the Windows kernel is only 4. And most of the classes on your link or wrappers are on them, or .NET analogues, which do the same, but a little differently. The author still asked the basics, and I showed the most frequently used primitives for synchronization. - Zergatul