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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:47:06+00:00 2026-06-03T20:47:06+00:00

Are the basic arithmetic operations Thread safe? For example, if there is ++ operation

  • 0

Are the basic arithmetic operations Thread safe?

For example, if there is ++ operation on a global variable, which will be modified from different threads, is it necessary to a lock around it?

For example

void MyThread() // can have many running instances
{
    aGlobal++;
}

or should it be

void MyThread()
{
    lock( lockerObj)
    {
        aGlobal++;
    }
}
  • 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-03T20:47:08+00:00Added an answer on June 3, 2026 at 8:47 pm

    The spec sums it up very well. Section 5.5, “Atomicity of variable references”:

    Reads and writes of the following data types are atomic: bool, char,
    byte, sbyte, short, ushort, uint, int, float, and reference types. In
    addition, reads and writes of enum types with an underlying type in
    the previous list are also atomic. Reads and writes of other types,
    including long, ulong, double, and decimal, as well as user-defined
    types, are not guaranteed to be atomic. Aside from the library
    functions designed for that purpose, there is no guarantee of atomic
    read-modify-write, such as in the case of increment or decrement.

    Conclusions:

    • Independent reads/writes are atomic (but only for some data types)
    • Read/modify/write (such as i++) is never atomic
    • You can use the Interlocked class methods to achieve atomicity when it’s not already guaranteed

    In cases where Interlocked functionality is not enough there is no other option than to use a synchronization primitive, such as Monitor.Enter (which the compiler also exposes through the lock statement).

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

Sidebar

Related Questions

What is the Big-O complexity for widespread algorithms of the basic arithmetic operations like
I have a basic question about assembly. Why do we bother doing arithmetic operations
I'm looking for tutorials which talk about bitwise arithmetic operations, such as addition, subtraction,
I need to define a class that guarantees the basic numeric operations will be
How can I store a basic arithmetic operator in a variable? I'd like to
Basic question - is it possible to access the current Page from a static
Basic question.. had to ask. Any help will be appreciated. Q: Why can't I
I have designed a quick and basic vector arithmetic library in C++. I call
I have a class that dynamically overloads basic arithmetic operators like so... import operator
Ive been trying to teach myself some very basic assembly (output to console, Arithmetic).

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.