Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8196879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:37:05+00:00 2026-06-07T05:37:05+00:00

Does the c# lock keyword use a ‘yielding’, ‘spin-locking’ or hybrid approach to handle

  • 0

Does the c# lock keyword use a ‘yielding’, ‘spin-locking’ or hybrid approach to handle contention?

So far my searches on the .net lock statement hasn’t turned up an answer.
I will post if I do find any more.
So far all I could find is When should one use a spinlock … with a nicely worded accepted answer by Mecki.

But I am looking for some definitive answer or documentation regarding .net/c# if anyone has one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:37:06+00:00Added an answer on June 7, 2026 at 5:37 am

    Following code:

    lock (_syncRoot)
    {
        // Do stuff
    }
    

    Is translated by the compiler to:

    Monitor.Enter(_syncRoot)
    try
    {
        // Do stuff
    }
    finally
    {
        Monitor.Exit(_syncRoot);
    }
    

    This is the naive (and old) implementation, actually with .NET 4.0 the implementation is more or less this (see Eric’s blog for complete reference):

    bool locked = false;
    try
    {
        Monitor.Enter(_syncRoot, ref locked);
    }
    finally
    {
        if (locked)
            Monitor.Exit(_syncRoot);
    }
    

    EDITED

    That said the question is how Monitor.Enter() works? Well, default Mono implementation uses a semaphore to acquire the lock but Microsoft .NET implementation acts different.

    I was reading Concurrent Windows Programming (by Joe Duffy) when a paragraph did catch my attention, my first answer said “no, it doesn’t use spinning because performance may not be good in general cases”. Correct answer is “yes, .NET Monitor uses spinning”. Both .NET Monitor and Windows Critical Sections perform a short spinning before falling back to a true wait on a kernel object. This algorithm is called “two-phase locking protocol” and it’s appropriate because context switches and kernel transitions are very expansive, on a multiprocessor machine spinning can avoid both of them.

    Moreover do not forget these are implementation details and can change in any release (or algorithm can be different for different hardwares because of JIT compiler).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when using a lock does the thing you are locking on have to be
This is from MSDN: The lock keyword ensures that one thread does not enter
When we say we lock on an object using the synchronized keyword, does it
Does calling a destructor of an boost::lock object explicitly have any consequence at all?
Does managed C++ have an equivalent to C#'s lock() and VB's SyncLock? If so,
How does Apache (most popular version nowadays, i guess) handle a connection to a
Why does IIS lock my ASP.NET website folders? I can't view the folder contents
Does the typical malloc (for x86-64 platform and Linux OS) naively lock a mutex
Is this ValueStore class threadsafe? Does the lock scope in GetInt(string key) need to
Possible Duplicate: How does @synchronized lock/unlock in Objective-C? I have an application that creates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.