Skip to content

A C# Pill that tries to peek under the hood of the `lock` instruction. Is the `lock` statement just a syntactic sugar for the usage of `Monitor` class?

License

Notifications You must be signed in to change notification settings

CSharp-Pills-15mg/Lock-vs-Monitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lock vs Monitor

Pill Category

Language (C#)

Description

Concurrent access

When there are multiple threads that must access the same resource, it is mandatory to use a synchronization mechanism, otherwise we may loos data or leave the resource in an inconsistent state.

The lock statement

The simplest synchronization mechanism provided by the C# language is the lock statement.

lock (obj)
{
	// do something
}

But, what is there, under the hood of this lock statement? Is there something that is worth mentioning? Microsoft's documentation says that the Monitor class is used, under the hood, to obtain and release the lock. Is that true?

This C# Pill is targeting to answer the following question.

Question

  • Is the lock statement just a syntactic sugar for the usage of Monitor class?

Donations

If you like my work and want to support me, you can buy me a coffee:

ko-fi

About

A C# Pill that tries to peek under the hood of the `lock` instruction. Is the `lock` statement just a syntactic sugar for the usage of `Monitor` class?

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Languages