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.
- Look at the thread pool - MBo
- code sample with threads and sync? - slippyk
- Yes, I just want to understand that there are synchronization primitives on an example. - Vladimr Vladimirovoch
- ThreadingPool docs.microsoft.com/ru-ru/dotnet/api/… - B. Vandyshev
- About synchronization, most likely talking about lock-ah docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/… - B. Vandyshev
|
1 answer
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:
- Event. In .NET there is a wrapper in the form of classes
AutoResetEvent/ManualResetEvent. Something similar can be done usingMonitor.Pulse()andMonitor.Wait(). But Windows objects are more functional. - Mutex With the same class wrapper. There is a similar functionality in .NET itself, in the methods of the
Monitorclass or in theC# lockoperator. - Semaphore. With the same class wrapper.
- 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
- @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
|