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 8251651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:18:21+00:00 2026-06-08T00:18:21+00:00

I have the following Lock statement: private readonly object ownerLock_ = new object(); lock

  • 0

I have the following Lock statement:

private readonly object ownerLock_ = new object();

lock (ownerLock_)
{
}

Should I use volatile keyword for my lock variable?

private readonly volatile object ownerLock_ = new object();

On MSDN I saw that it usually used for a field that is accessed without locking, so if I use Lock I don’t need to use volatile?

From MSDN:

The volatile modifier is usually used for a field that is accessed by
multiple threads without using the lock statement to serialize access.

  • 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-08T00:18:23+00:00Added an answer on June 8, 2026 at 12:18 am

    If you’re only ever accessing the data that the lock “guards” while you own the lock, then yes – making those fields volatile is superfluous. You don’t need to make the ownerLock_ variable volatile either. (You haven’t currently shown any actual code within the lock statement, which makes it hard to talk about in concrete terms – but I’m assuming you’d actually be reading/modifying some data within the lock statement.)

    volatile should be very rarely used in application code. If you want lock-free access to a single variable, Interlocked is almost always simpler to reason about. If you want lock-free access beyond that, I would almost always start locking. (Or try to use immutable data structures to start with.)

    I’d only expect to see volatile within code which is trying to build higher level abstractions for threading – so within the TPL codebase, for example. It’s really a tool for experts who really understand the .NET memory model thoroughly… of whom there are very few, IMO.

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

Sidebar

Related Questions

I have the following code public class Test { Lock lock = new ReentrantLock();
Suppose i have following code Rectangle rect = new Rectangle(); lock(rect) { ---- ---
Consider you have the following code: 1. Why do we use double check lock,
I have the following code: def query(self,query): lock = QMutexLocker(self.mutex) reply = self.conn.query(query) if
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
If I have the following code: lock(SyncRoot){ return value; } Will the SyncRoot be
I have following scenario. Some people use approach 1 and some use approach 2.
Hey guys. I have the following models: Category Event Lock Item Site User Events
I have the following helper class (simplified): public static class Cache { private static
I have code like the following: class SomeSharedData(object): def __init__(self): self._lock = RLock() self._errors

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.