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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:32:10+00:00 2026-05-20T12:32:10+00:00

I’ve gotten myself into a confused mess regarding multithreaded programming and was hoping someone

  • 0

I’ve gotten myself into a confused mess regarding multithreaded programming and was hoping someone could come and slap some understanding in me.

After doing quite a bit of reading, I’ve come to the understanding that I should be able to set the value of a 64 bit int atomically on a 64 bit system1.

I found a lot of this reading difficult though, so thought I would try to make a test to verify this. So I wrote a simple program with one thread which would set a variable into one of two values:

bool switcher = false;

while(true)
{
    if (switcher)
        foo = a;
    else
        foo = b;
    switcher = !switcher;
}

And another thread which would check the value of foo:

while (true)
{
    __uint64_t blah = foo;
    if ((blah != a) && (blah != b))
    {
        cout << "Not atomic! " << blah << endl;
    }
}

I set a = 1844674407370955161; and b = 1144644202170355111;. I run this program and get no output warning me that blah is not a or b.

Great, looks like it probably is an atomic write…but then, I changed the first thread to set a and b directly, like so:

bool switcher = false;

while(true)
{
    if (switcher)
        foo = 1844674407370955161;
    else
        foo = 1144644202170355111;
    switcher = !switcher;
}

I re-run, and suddenly:

Not atomic! 1144644203261303193
Not atomic! 1844674406280007079
Not atomic! 1144644203261303193
Not atomic! 1844674406280007079

What’s changed? Either way I’m assigning a large number to foo – does the compiler handle a constant number differently, or have I misunderstood everything?

Thanks!


1: Intel CPU documentation, section 8.1, Guaranteed Atomic Operations

2: GCC Development list discussing that GCC doesn’t guarantee it in the documentation, but the kernel and other programs rely on it

  • 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-20T12:32:11+00:00Added an answer on May 20, 2026 at 12:32 pm

    Disassembling the loop, I get the following code with gcc:

    .globl _switcher
    _switcher:
    LFB2:
        pushq   %rbp
    LCFI0:
        movq    %rsp, %rbp
    LCFI1:
        movl    $0, -4(%rbp)
    L2:
        cmpl    $0, -4(%rbp)
        je  L3
        movq    _foo@GOTPCREL(%rip), %rax
        movl    $-1717986919, (%rax)
        movl    $429496729, 4(%rax)
        jmp L5
    L3:
        movq    _foo@GOTPCREL(%rip), %rax
        movl    $1486032295, (%rax)
        movl    $266508246, 4(%rax)
    L5:
        cmpl    $0, -4(%rbp)
        sete    %al
        movzbl  %al, %eax
        movl    %eax, -4(%rbp)
        jmp L2
    LFE2:
    

    So it would appear that gcc does use to 32-bit movl instruction with 32-bit immediate values. There is an instruction movq that can move a 64-bit register to memory (or memory to a 64-bit register), but it does not seems to be able to set move an immediate value to a memory address, so the compiler is forced to either use a temporary register and then move the value to memory, or to use to movl. You can try to force it to use a register by using a temporary variable, but this may not work.

    References:

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

Sidebar

Related Questions

No related questions found

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.