Thursday, July 25, 2019

Summary for synchronization in .NET

The following tables summarize the .NET tools available for coordinating or synchronizing the actions of threads:

1 Simple Blocking Methods
    
ConstructPurpose
SleepBlocks for a given time period
JoinWaits for another thread to finish

2 Locking Constructs
ConstructPurposeCross Process?Speed
LockEnsures just one thread can access a resource, or section of code.NoFast
MutexEnsures just one thread can access a resource, or section of code. Can be used to prevent multiple instances of an application from starting.YesModerate
SemaphoreEnsures not more than a pecified number of therads can access a resource, or section of code.YesModerate

(Synchronization Context are also provided, for automatic locking)
3 Signaling Constructs
ConstructPurposeCross Process?Speed
EventWaitHandleAllows a thread to wait until it receives a signal from another therad.YesModerate
Wait & PulseAllows a thread to wait until a custom blocking condition is met.NoModerate
4 Non-Blocking Synchronization Constructs
ConstructPurposeCross Process?Speed
InterlockedTo perform simple non-blocking atomic operations.Yes (Assuming shared memory)Very fast
volatileTo allow safe non-blocking access to individual fields outside of a lock.Yes (Assuming shared memory)Very fast

No comments:

Post a Comment