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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:04:47+00:00 2026-05-23T15:04:47+00:00

Consider the following sample code: class MyClass { public long x; public void DoWork()

  • 0

Consider the following sample code:

class MyClass
{
    public long x;

    public void DoWork()
    {
        switch (x)
        {
            case 0xFF00000000L:
                // do whatever...
                break;

            case 0xFFL:
                // do whatever...
                break;

            default:
                //notify that something going wrong
                throw new Exception();
        }
    }
}

Forget the uselessness of the snippet: my doubt is about the behavior of the switch statement.

Suppose that the x field could have only two values: 0xFF00000000L or 0xFFL. The switch above should not fall into the “default” option.

Now imagine that one thread is executing the switch with “x” equal to 0xFFL, thus the first condition won’t match. At the same time, another thread modifies the “x” variable to 0xFF00000000L. We know a 64-bit operation is not atomic, so that the variable will have the lower dword zeroed first, then the upper set afterward (or vice versa).

If the second condition in the switch will be done when the “x” is zero (i.e. during the new assignment), will we fall into the undesired “default” case?

  • 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-05-23T15:04:47+00:00Added an answer on May 23, 2026 at 3:04 pm

    You’re actually posting two questions.

    Is it threadsafe?

    Well, obviously it is not, another thread might change the value of X while the first thread is going into the switch. Since there’s no lock and the variable is not volatile you’ll switch based on the wrong value.

    Would you ever hit the default state of the switch?

    Theoretically you might, as updating a 64 bits is not an atomic operation and thus you could theoretically jump in the middle of the assignment and get a mingled result for x as you point out. This statistically won’t happen often but it WILL happen eventually.

    But the switch itself is threadsafe, what’s not threadsafe is read and writes over 64 bit variables (in a 32 bit OS).

    Imagine instead of switch(x) you have the following code:

    long myLocal = x;
    switch(myLocal)
    {
    }
    

    now the switch is made over a local variable, and thus, it’s completely threadsafe. The problem, of course, is in the myLocal = x read and its conflict with other assignments.

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

Sidebar

Related Questions

Consider the following sample code: class SampleClass { public long SomeProperty { get; set;
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Consider the following code: class A { public: A() {} ~A() {} }; class
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following sample code which uses a TrustManager to log whether an outgoing
Consider the following code sample: interface IData { int Count(); } interface IOperations {
Consider the following code: class Program { static Program() { Program.program1.Value = 5; }

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.