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

  • Home
  • SEARCH
  • 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 7937789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:36:49+00:00 2026-06-03T22:36:49+00:00

ive been asking myself : why should i use lock to only one statement…

  • 0

ive been asking myself : “why should i use lock to only one statement”…

(IMHO – if its 1 operation only like an assignment – so there shouldnt be a problem..)?

then i saw this :

As a basic rule, you need to lock around accessing any writable shared
field. Even in the simplest case—an assignment operation on a single
field—you must consider synchronization. In the following class,
neither the Increment nor the Assign method is thread-safe:

class ThreadUnsafe
{
  static int _x;
  static void Increment() { _x++; }  
  static void Assign() { _x = 123; }
}

can you please tell me why this is not thread safe ?
ive been running many scripts in my head and couldnt find any problem…

  • 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-03T22:36:50+00:00Added an answer on June 3, 2026 at 10:36 pm

    Here’s an example of why your example is not thread-safe. Initially, _x = 0. Let’s say you run Increment and Assign in parallel. If the methods were thread-safe, the result should be either 100 (if increment is executed before assign) or 101 (if increment is executed after assign).

    (EDIT: Note that each thread has it’s own working stack!)

     Thread 1 (executing Increment)    Thread 2 (executing Assign 100)
     -----------------------------------------------------------------
     read _x onto stack       (= 0)
                                       put 100 on top of stack
                                       write top of stack to _x (= 100)
     increment top of stack   (= 1)
     write top of stack to _x (= 1)
    

    _x is now 1, which is neither 100 nor 101.

    Of course, it could be that your incrementation method is compiled into single, atomic operation by the compiler. But you cannot rely on this, unless it is specifically guaranteed by the compiler that you use.


    If you use a lock, the following happens:

     Thread 1 (executing Increment)    Thread 2 (executing Assign 100)
     -----------------------------------------------------------------
     lock (success)
     read _x onto stack       (= 0)
                                       lock (lock already taken; 
                                       |     wait until Thead 1's lock is released)
     increment top of stack   (= 1)    |
     write top of stack to _x (= 1)    |
     unlock                            |
                                       +> (success)
                                       put 100 on top of stack
                                       write top of stack to _x (= 100)
                                       unlock
    

    The result is now 100. Basically, the lock ensures that two locked blocks do not overlap.

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

Sidebar

Related Questions

Ive been playing around with delegates trying to learn and I ran into one
I have been using analytics software for a while, and I've been asking myself
I've been trying to create a layout like this: Now I'm not asking for
this question I've been asking myself since the day I started programming in MVC
I've been asking myself this question for quite some time and I haven't found
I've been fighting with this one for a while and i've finally brought myself
So I have been asking questions here trying to get the answer for myself
I've been asking a question or two over the past few days of working
I feel stupid asking this question, but I've been wrestling with it for hours
(I'm also asking this on the OpenRasta google group) Hey all, I've been using

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.