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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:33:02+00:00 2026-06-01T19:33:02+00:00

I read about lock, though not understood nothing at all. My question is why

  • 0

I read about lock, though not understood nothing at all.
My question is why do we use a un-used object and lock that and how this makes something thread-safe or how this helps in multi-threading ? Isn’t there other way to make thread-safe code.

public class test {
    private object Lock { get; set; }

    ...
    lock (this.Lock) { ... }
    ...
}

Sorry is my question is very stupid, but i don’t understand, although i’ve used it many times.

  • 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-01T19:33:03+00:00Added an answer on June 1, 2026 at 7:33 pm

    Accessing a piece of data from one thread while other thread is modifying it is called “data race condition” (or just “data race”) and can lead to corruption of data. (*)

    Locks are simply a mechanism for avoiding data races. If two (or more) concurrent threads lock the same lock object, then they are no longer concurrent and can no longer cause data races, for the duration of the lock. Essentially, we are serializing the access to shared data.

    The trick is to keep your locks as “wide” as you must to avoid data races, yet as “narrow” as you can to gain performance through concurrent execution. This is a fine balance that can easily go out of whack in either direction, which is why multi-threaded programming is hard.

    Some guidelines:

    • As long all threads are just reading the data and none will ever modify it, lock is unnecessary.
    • Conversely, if at least one thread might at some point modify the data, then all concurrent code paths accessing that same data must be properly serialized through locks, even those that only read the data.
      • Using a lock in one code path but not the other will leave the data wide open to race conditions.
      • Also, using one lock object in one code path, but a different lock object in another (concurrent) code path does not serialize these code paths and leaves you wide open to data races.
      • On the other hand, if two concurrent code paths access different data, they can use different lock objects. But, whenever there is more than one lock object, watch out for deadlocks. A deadlock is often also a “code race condition” (and a heisenbug, see below).
    • The lock object does not need to be (and usually isn’t) the same thing as the data you are trying to protect. Unfortunately, there is no language facility that lets you “declare” which data is protected by which lock object, so you’ll have to very carefully document your “locking convention” both for other people that might maintain your code, and for yourself (since even after a short time you will forget some of the nooks and crannies of your locking convention).
    • It’s usually a good idea to protect the lock object from the outside world as much as you can. After all, you are using it for the very sensitive task of locking and you don’t want it locked by external actors in unforeseen ways. That’s why using this or a public field as a lock object is usually a bad idea.
    • The lock keyword is simply a more convenient syntax for Monitor.Enter and Monitor.Exit.
    • The lock object can be any object in .NET, but value objects will be boxed in the call to Monitor.Enter, which means threads will not share the same lock object, leaving the data unprotected. Therefore, only use reference types as lock objects.
    • For inter-process communication you can use a global mutex, which can be created by passing a non-empty name to Mutex Constructor. Global mutexes provide essentially the same functionality as regular “local” locking, except they can be shared between separate processes.
    • There are synchronization mechanisms other than locks, such as semaphores, condition variables, message queues or atomic operations. Be careful when mixing different synchronization mechanisms.
    • Locks also behave as memory barriers, which is increasingly important on modern multi-core, multi-cache CPUs. This is part of the reason why you need locks on reading the data and not just writing.

    (*) It is called “race” because concurrent threads are “racing” towards performing an operation on the shared data and whoever wins that race determines the outcome of the operation. So the outcome depends on timing of the execution, which is essentially random on modern preemptive multitasking OSes. Worse yet, timing is easily modified by a simple act of observing the program execution through tools such as debugger, which makes them “heisenbugs” (i.e. the phenomenon being observed is changed by the mere act of observation).

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

Sidebar

Related Questions

This question is NOT about race-conditions, atomicity, or why you should use locks in
Read about the issue in this stackoverflow question . Still have the same issue
Ive read about it and to be honest it all seems like a bunch
I read about small talk being completely object oriented.. is C++ also completely object
I read about an inheritance feature in PostgreSQL that seemed pretty neat. Unfortunately I
I read about ContactsContract.CommonDataKinds.GroupMembership , but I can't figure out what URI use to
Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php .
I know that android emulator is to slow, and I read a lot about
First of all, I know there are a few other StackOverflow questions about this
There's this snazzy captcha that asks questions rather than displaying pictures. You can read

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.