SlideShare a Scribd company logo
1 of 16
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

What's hot

Multithreading
Multithreading Multithreading
Multithreading WafaQKhan
 
Transaction management
Transaction managementTransaction management
Transaction managementrenuka_a
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3Doris Chen
 
Introduction to theory of computation
Introduction to theory of computationIntroduction to theory of computation
Introduction to theory of computationVinod Tyagi
 
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)techlovers3
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threadingAPU
 
Html interview-questions-and-answers
Html interview-questions-and-answersHtml interview-questions-and-answers
Html interview-questions-and-answersMohitKumar1985
 
Complete Lecture on Css presentation
Complete Lecture on Css presentation Complete Lecture on Css presentation
Complete Lecture on Css presentation Salman Memon
 
Concurrency vs parallelism
Concurrency vs parallelismConcurrency vs parallelism
Concurrency vs parallelismYiwei Gong
 
Introduction to Basic Concepts in Web
Introduction to Basic Concepts in WebIntroduction to Basic Concepts in Web
Introduction to Basic Concepts in WebJussi Pohjolainen
 

What's hot (20)

Sending emails through PHP
Sending emails through PHPSending emails through PHP
Sending emails through PHP
 
Multithreading
Multithreading Multithreading
Multithreading
 
Transaction management
Transaction managementTransaction management
Transaction management
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
 
Introduction to theory of computation
Introduction to theory of computationIntroduction to theory of computation
Introduction to theory of computation
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
Protocols and standards (http , html, xhtml, cgi, xml, wml, c html, etc)
 
Java threads
Java threadsJava threads
Java threads
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
Html interview-questions-and-answers
Html interview-questions-and-answersHtml interview-questions-and-answers
Html interview-questions-and-answers
 
Network topology
Network topologyNetwork topology
Network topology
 
Html basics
Html basicsHtml basics
Html basics
 
Complete Lecture on Css presentation
Complete Lecture on Css presentation Complete Lecture on Css presentation
Complete Lecture on Css presentation
 
internet protocols
internet protocolsinternet protocols
internet protocols
 
Concurrency vs parallelism
Concurrency vs parallelismConcurrency vs parallelism
Concurrency vs parallelism
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Pthread
PthreadPthread
Pthread
 
Introduction to Basic Concepts in Web
Introduction to Basic Concepts in WebIntroduction to Basic Concepts in Web
Introduction to Basic Concepts in Web
 
Transport layer
Transport layerTransport layer
Transport layer
 
Bridge
BridgeBridge
Bridge
 

Similar to C# Thread synchronization

Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docxssuser1c8ca21
 
Multithreading and concurrency.pptx
Multithreading and concurrency.pptxMultithreading and concurrency.pptx
Multithreading and concurrency.pptxShymmaaQadoom1
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronizationpriyabogra1
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3DHIRAJ PRAVIN
 
slides8 SharedMemory.ppt
slides8 SharedMemory.pptslides8 SharedMemory.ppt
slides8 SharedMemory.pptaminnezarat
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
Reactive programming with RxAndroid
Reactive programming with RxAndroidReactive programming with RxAndroid
Reactive programming with RxAndroidSavvycom Savvycom
 
Storm Real Time Computation
Storm Real Time ComputationStorm Real Time Computation
Storm Real Time ComputationSonal Raj
 

Similar to C# Thread synchronization (20)

Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
 
systemverilog-interview-questions.docx
systemverilog-interview-questions.docxsystemverilog-interview-questions.docx
systemverilog-interview-questions.docx
 
Multithreading and concurrency.pptx
Multithreading and concurrency.pptxMultithreading and concurrency.pptx
Multithreading and concurrency.pptx
 
Tech talk
Tech talkTech talk
Tech talk
 
Multithreading
MultithreadingMultithreading
Multithreading
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Thread syncronization
Thread syncronizationThread syncronization
Thread syncronization
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Android development training programme , Day 3
Android development training programme , Day 3Android development training programme , Day 3
Android development training programme , Day 3
 
Multi Threading
Multi ThreadingMulti Threading
Multi Threading
 
slides8 SharedMemory.ppt
slides8 SharedMemory.pptslides8 SharedMemory.ppt
slides8 SharedMemory.ppt
 
unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
 
Storm
StormStorm
Storm
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Rxandroid
RxandroidRxandroid
Rxandroid
 
RxAndroid
RxAndroidRxAndroid
RxAndroid
 
Reactive programming with RxAndroid
Reactive programming with RxAndroidReactive programming with RxAndroid
Reactive programming with RxAndroid
 
Storm Real Time Computation
Storm Real Time ComputationStorm Real Time Computation
Storm Real Time Computation
 

More from Prem Kumar Badri

Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexersPrem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopePrem Kumar Badri
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and eventsPrem Kumar Badri
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objectsPrem Kumar Badri
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variablesPrem Kumar Badri
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsPrem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingPrem Kumar Badri
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsPrem Kumar Badri
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parametersPrem Kumar Badri
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variablesPrem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformPrem Kumar Badri
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collectionPrem Kumar Badri
 

More from Prem Kumar Badri (20)

Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
 
Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexers
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
Module 11 : Inheritance
Module 11 : InheritanceModule 11 : Inheritance
Module 11 : Inheritance
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variables
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & Exceptions
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
 
Module 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
 
C# Multi threading
C# Multi threadingC# Multi threading
C# Multi threading
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collections
 

Recently uploaded

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

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(); …