0% found this document useful (0 votes)
23 views

Locks Mutex Semaphore

This document discusses various techniques for thread synchronization in a multithreaded environment. It describes locks, monitors, mutexes, and semaphores as mechanisms for controlling access to shared resources and ensuring thread safety. Locks allow only one thread to access a critical section of code at a time. Monitors provide methods for entering and exiting a protected region. Mutexes prevent access from external threads. Semaphores permit a specified number of threads to access a resource simultaneously. The document also notes some common uses and disadvantages of multithreading, including maintaining responsive user interfaces, parallel processing, and added complexity from thread interaction.

Uploaded by

reshmaitagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Locks Mutex Semaphore

This document discusses various techniques for thread synchronization in a multithreaded environment. It describes locks, monitors, mutexes, and semaphores as mechanisms for controlling access to shared resources and ensuring thread safety. Locks allow only one thread to access a critical section of code at a time. Monitors provide methods for entering and exiting a protected region. Mutexes prevent access from external threads. Semaphores permit a specified number of threads to access a resource simultaneously. The document also notes some common uses and disadvantages of multithreading, including maintaining responsive user interfaces, parallel processing, and added complexity from thread interaction.

Uploaded by

reshmaitagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

THREAD SYNCHRONISATION

TECHNIQUES
Thread
Thread States
Lock
It ensures only one thread to be executed in given moment of time.
The lock keyword marks a statement block as a critical section by obtaining the mutual-
exclusion lock for a given object, executing a. statement and then releasing the lock
Monitor
Lock keyword is just a short-form of Monitor.
The Monitor class has the following methods for the synchronize access to a region of code by taking and
releasing a lock:

Monitor.Enter
Monitor.TryEnter
Monitor.Exit.

var lockObj = new Object();


if (Monitor.TryEnter(lockObj)) {
try {
// The critical section.
}
finally {
// Ensure that the lock is released.
Monitor.Exit(lockObj);
}
}
else {
// The lock was not axquired.
}
Mutex
Mutex provides safety against the external threads.
Semaphore
Semaphore allows one or more threads to enter to executes their task with thread safety.
Summary
Threadings Uses and Misuses
Maintaining a responsive user interface.
Making efficient use of an otherwise blocked CPU.
Parallel programming.
Allowing requests to be processed simultaneously.

Disadvantages:
Interaction between threads add complexity
Creation / tear down cost.
To be continued..
SemaphoreSlim
ReaderWriter locks
Interlocked class
SpinLocks
Deadlocks

You might also like