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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:17:07+00:00 2026-06-15T19:17:07+00:00

private const int Total = 500000; private static volatile int _count = 0; private

  • 0
    private const int Total = 500000;
    private static volatile int _count = 0;

    private static void Main()
    {
        Task task = Task.Factory.StartNew(Decrement);
        for (int i = 0; i < Total; i++)
        {
            _count++;
        }

        task.Wait();
        Console.WriteLine(_count);

        Console.ReadLine();
    }

    private static void Decrement()
    {
        for (int i = 0; i < Total; i++)
        {
            _count--;
        }
    }

Sometimes the result is 0,sometimes the result is -xxxxxx. I don’t know why. Can anybody explain it and tell me the correct usage.

  • 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-15T19:17:08+00:00Added an answer on June 15, 2026 at 7:17 pm

    volatile guarantees no reordering of operations and disable caching optimizations, but does not guarantees thread safety – so you can get any result from 0 to -Total (if every pair of -- and ++ are properly mixed). I’ve covered “preper mixing” in answer to Why the following C# multi-threaded code does not output zero though it does in debugger?.

    volatile is useful when you expect someone else to modify value so your code always reads reasonably recent value, which also will be consistent (i.e. for ++ you’ll not get int consisting of high part of 0xffff and low part of (0xffff+1) – you’ll get either one or another) since volatile is only applicable to types that are writen atomically.

    If you want to modify counter in 2 threads – use Interlocked.Increment/Interlocked.Decrement.

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

Sidebar

Related Questions

With this code: private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { const int TICKETSOURCE_COLUMN =
#include<iostream> using namespace std; class A { private: const int a=9; public: void display()
The naming convention for constants in C# is Pascal casing: private const int TheAnswer
I adh some constants in my project. private const int refercePosition = 0.3; I
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
private void button1_Click(object sender, EventArgs e) { for (int i = 0; i <
So in C# I create something like private const int HEADER_LENGTH = 13; private
I have tried to click on the button using the follow: private const int
I'm using the following code to query my database: private const int PAGE_SIZE =
Why can't i use const values in timeout attribute, example: private const int TIME_OUT

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.