Dew Math for .NET
|
Enables threads in a multithreaded application a single-thread only exclusive execution of a part of the code.
Math387.cs
Critical sections in general safeguard memory access operations in a piece of code, which might cause corruption, if they are executed concurrently by multiple threads at the same time. They are used always when there is a need to share resource, like for example memory. Once a thread enters a critical section, by calling the Enter method, all other threads will wait at the "Enter" entry, until "Leave" is called. At this point the next thread (one at a time), is allowed to continue past the "Enter" entry.
The sorted critical section implementation is a FIFO design (First-In-First-Out) and will pass threads strictly in the order in which they arrive. When there are many (8, 16 or more) threads all passing through the same critical section, the ability to guarantee the order in which they pass, leads to substantial performance improvements. The Windows OS standard critical section implementations (up until year 2022) throw a dice each time a thread enters sleep, because the sleep time is not deterministic. This can lead to starvation of some threads, which might never get to "Enter", while others have entered the protected code section multiple times already.
If each thread needs to allocate 2 pieces of memory, and one thread is starved for this, but all threads need to finish to finish the job, then the execution time of multi-threaded code will be extended proportionally.
Important: TFiFoCriticalSection performance will deliver best results, when the launched thread count within the process does not exceed hyper-threaded thread count of the system. Threads, which exceed this number, will sleep until one of the executing threads enters sleep or exits (Windows OS kernel as-designed feature). If launched thread count is very high. this can lead to (uneccessary), wakes of threads, which otherwise sleep anyway, to find the right one to continue the execution.
Copyright (c) 1999-2024 by Dew Research. All rights reserved.
|
What do you think about this topic? Send feedback!
|