SlideShare a Scribd company logo
THREAD SYNCHRONIZATION
THREAD
 a single flow of execution logic in a program.
 Enable your program to perform concurrent
processing.
 Increases efficiency and responsiveness.
 Share resources.
 Unpredictable execution order.
SYECHROIZATION
 Simultaneous access to shared resource can cause
inconsistent and incorrect output.
 Introduce resource-sharing issues such as
deadlocks and race conditions
 So, the need of synchronized access to the shared
resources.
 System. Threading namespace.
PROBLEM
 Two threads updating a single shared variable
 One thread wants to decrement amount by 10K
 The other thread wants to decrement amount by 50%
amount= 100000;
…
…
amount = 0.50 * amount;
…
…
…
…
amount= amount - 10000;
…
…
RESULT
UNPREDICATBLE
(CASE 1)
…
r1 = load from amount
r1 = r1 - 10000;
store r1 to amount
…
…
r1 = load from amount
r1 = 0.5 * r1
store r1 to amount
…
amount= 100000;
amount= ?
(CASE 2)
…
r1 = load from amount
r1 = r1 - 10000;
store r1 to amount
…
…
r1 = load from amount
r1 = 0.5 * r1
store r1 to amount
…
amount= 100000;
amount= ?
(CASE 3)
…
r1 = load from amount
r1 = r1 - 10000;
store r1 to amount
…
…
r2 = load from amount
r2 = 0.5 * r2
store r2 to amount
…
amount= 100000;
amount= ?
SOLUTION
1. Lock
2. Monitor
3. Mutex
4. Semaphore
LOCK
 The lock allows us to define a scope of code that
must be synchronized between threads so that
incoming threads cannot interrupt the current
thread.
 Syntax-
…
lock(ThreadLock)
{
// Critical Section
}
….
MONITOR
 The lock is just a shorthand notation for working
with the System.Threading.Monitor class.
 Syntax-
Monitor.Enter(threadLock);
…
//Critical Section
…
Monitor.Exit(threadLock);
WHY MONITOR ???
 If we use the Monitor, we're able to tell the active
thread to wait by using Wait() method and let the
waiting threads know when the current thread is
completed via Pulse() and PulseAll() methods.
MUTEX
 A Mutex is similar to the lock, however, it can work
across multiple processes. But it is slower than
the lock.
 Mutex allows us to call the WaitOne() method to
lock and ReleaseMutex() to unlock.
 a Mutex can be released only from the same
thread which obtained it.
 The primary use for a cross-process Mutex is to
ensure that only one instance of a program can run
at a time
MUTEX
 Syntax-
static Mutex mutex = new Mutex();
Mutex.waitone()
…..
…..
Mutex.ReleaseMutex();
SEMAPHORE
 A Semaphore with a capacity of one is like
a Mutex or lock. However, the Semaphore has
no owner.
 We can use Semaphores to limit concurrency by
preventing too many threads from executing a
particular piece of code at once.
 Syntax-
// 4 available from capacity of 5 static
Semaphore semaphore = new Semaphore(4,5);
…
semaphore.WaitOne();
//critical section
semaphore.Release();
…
C# Thread synchronization

More Related Content

PPT
Threads And Synchronization in C#
PPT
System call
PPT
Socket System Calls
PDF
Pipelining and ILP (Instruction Level Parallelism)
PPTX
Distributed Shared Memory notes in distributed systems.pptx
PDF
Inheritance In Java
PPTX
Message passing ( in computer science)
PPTX
Message Queuing (MSMQ)
Threads And Synchronization in C#
System call
Socket System Calls
Pipelining and ILP (Instruction Level Parallelism)
Distributed Shared Memory notes in distributed systems.pptx
Inheritance In Java
Message passing ( in computer science)
Message Queuing (MSMQ)

What's hot (20)

PPTX
Java - Exception Handling
PPTX
Chorus - Distributed Operating System [ case study ]
PDF
Sending emails through PHP
PPTX
Operating System- Multilevel Paging, Inverted Page Table
PPTX
Peephole optimization
PPTX
Basic blocks - compiler design
PPT
Unit 1 Java
PPTX
iOS Memory Management
PPT
distributed shared memory
PDF
DDBMS_ Chap 5 Semantic Data Control
PPT
Input output in linux
PPT
Memory management
PPTX
Networking Models
PPT
Shared memory
PPT
Kernel I/O Subsystem
PPT
Unix File System
PPTX
Code Optimization
PDF
Circuit Switching, Packet Switching, Virtual Circuit Networks and Datagram Ne...
PDF
8. mutual exclusion in Distributed Operating Systems
PPTX
System call
Java - Exception Handling
Chorus - Distributed Operating System [ case study ]
Sending emails through PHP
Operating System- Multilevel Paging, Inverted Page Table
Peephole optimization
Basic blocks - compiler design
Unit 1 Java
iOS Memory Management
distributed shared memory
DDBMS_ Chap 5 Semantic Data Control
Input output in linux
Memory management
Networking Models
Shared memory
Kernel I/O Subsystem
Unix File System
Code Optimization
Circuit Switching, Packet Switching, Virtual Circuit Networks and Datagram Ne...
8. mutual exclusion in Distributed Operating Systems
System call
Ad

Similar to C# Thread synchronization (20)

PPT
Multithreading Presentation
PDF
MultiThreading in Python
DOCX
systemverilog-interview-questions.docx
PPTX
Multithreading and concurrency.pptx
PPT
Tech talk
PPT
Multithreading
PPTX
.NET Multithreading/Multitasking
PPTX
Thread syncronization
PPT
Java Multithreading and Concurrency
PDF
Android development training programme , Day 3
PPTX
PPT
slides8 SharedMemory.ppt
PPTX
unit-3java.pptx
PPTX
PPT
Thread model in java
DOCX
Parallel Programming With Dot Net
PPTX
Rxandroid
PPTX
RxAndroid
PPTX
Reactive programming with RxAndroid
PDF
Storm Real Time Computation
Multithreading Presentation
MultiThreading in Python
systemverilog-interview-questions.docx
Multithreading and concurrency.pptx
Tech talk
Multithreading
.NET Multithreading/Multitasking
Thread syncronization
Java Multithreading and Concurrency
Android development training programme , Day 3
slides8 SharedMemory.ppt
unit-3java.pptx
Thread model in java
Parallel Programming With Dot Net
Rxandroid
RxAndroid
Reactive programming with RxAndroid
Storm Real Time Computation
Ad

More from Prem Kumar Badri (20)

PPTX
Module 15 attributes
PPTX
Module 14 properties and indexers
PPTX
Module 12 aggregation, namespaces, and advanced scope
PPTX
Module 13 operators, delegates, and events
PPTX
Module 11 : Inheritance
PPTX
Module 10 : creating and destroying objects
PPTX
Module 9 : using reference type variables
PPTX
Module 8 : Implementing collections and generics
PPTX
Module 7 : Arrays
PPTX
Module 6 : Essentials of Object Oriented Programming
PPTX
Module 5 : Statements & Exceptions
PPTX
Module 4 : methods & parameters
PPTX
Module 3 : using value type variables
PPTX
Module 2: Overview of c#
PPTX
Module 1 : Overview of the Microsoft .NET Platform
PPTX
C# Non generics collection
PPTX
C# Multi threading
PPT
C# Method overloading
PPTX
C# Inheritance
PPTX
C# Generic collections
Module 15 attributes
Module 14 properties and indexers
Module 12 aggregation, namespaces, and advanced scope
Module 13 operators, delegates, and events
Module 11 : Inheritance
Module 10 : creating and destroying objects
Module 9 : using reference type variables
Module 8 : Implementing collections and generics
Module 7 : Arrays
Module 6 : Essentials of Object Oriented Programming
Module 5 : Statements & Exceptions
Module 4 : methods & parameters
Module 3 : using value type variables
Module 2: Overview of c#
Module 1 : Overview of the Microsoft .NET Platform
C# Non generics collection
C# Multi threading
C# Method overloading
C# Inheritance
C# Generic collections

Recently uploaded (20)

PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Pre independence Education in Inndia.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharma ospi slides which help in ospi learning
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pre independence Education in Inndia.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
01-Introduction-to-Information-Management.pdf
Final Presentation General Medicine 03-08-2024.pptx
Cell Types and Its function , kingdom of life
Pharma ospi slides which help in ospi learning
Basic Mud Logging Guide for educational purpose
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Sports Quiz easy sports quiz sports quiz
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial disease of the cardiovascular and lymphatic systems

C# Thread synchronization

  • 2. THREAD  a single flow of execution logic in a program.  Enable your program to perform concurrent processing.  Increases efficiency and responsiveness.  Share resources.  Unpredictable execution order.
  • 3. SYECHROIZATION  Simultaneous access to shared resource can cause inconsistent and incorrect output.  Introduce resource-sharing issues such as deadlocks and race conditions  So, the need of synchronized access to the shared resources.  System. Threading namespace.
  • 4. PROBLEM  Two threads updating a single shared variable  One thread wants to decrement amount by 10K  The other thread wants to decrement amount by 50% amount= 100000; … … amount = 0.50 * amount; … … … … amount= amount - 10000; … …
  • 6. (CASE 1) … r1 = load from amount r1 = r1 - 10000; store r1 to amount … … r1 = load from amount r1 = 0.5 * r1 store r1 to amount … amount= 100000; amount= ?
  • 7. (CASE 2) … r1 = load from amount r1 = r1 - 10000; store r1 to amount … … r1 = load from amount r1 = 0.5 * r1 store r1 to amount … amount= 100000; amount= ?
  • 8. (CASE 3) … r1 = load from amount r1 = r1 - 10000; store r1 to amount … … r2 = load from amount r2 = 0.5 * r2 store r2 to amount … amount= 100000; amount= ?
  • 9. SOLUTION 1. Lock 2. Monitor 3. Mutex 4. Semaphore
  • 10. LOCK  The lock allows us to define a scope of code that must be synchronized between threads so that incoming threads cannot interrupt the current thread.  Syntax- … lock(ThreadLock) { // Critical Section } ….
  • 11. MONITOR  The lock is just a shorthand notation for working with the System.Threading.Monitor class.  Syntax- Monitor.Enter(threadLock); … //Critical Section … Monitor.Exit(threadLock);
  • 12. WHY MONITOR ???  If we use the Monitor, we're able to tell the active thread to wait by using Wait() method and let the waiting threads know when the current thread is completed via Pulse() and PulseAll() methods.
  • 13. MUTEX  A Mutex is similar to the lock, however, it can work across multiple processes. But it is slower than the lock.  Mutex allows us to call the WaitOne() method to lock and ReleaseMutex() to unlock.  a Mutex can be released only from the same thread which obtained it.  The primary use for a cross-process Mutex is to ensure that only one instance of a program can run at a time
  • 14. MUTEX  Syntax- static Mutex mutex = new Mutex(); Mutex.waitone() ….. ….. Mutex.ReleaseMutex();
  • 15. SEMAPHORE  A Semaphore with a capacity of one is like a Mutex or lock. However, the Semaphore has no owner.  We can use Semaphores to limit concurrency by preventing too many threads from executing a particular piece of code at once.  Syntax- // 4 available from capacity of 5 static Semaphore semaphore = new Semaphore(4,5); … semaphore.WaitOne(); //critical section semaphore.Release(); …