12

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

5
  • Did you get any warning when compiling?
    – Nim
    Mar 10, 2011 at 11:00
  • I don't think it's the culprit, but literals have int type by default, so you'd want 1844674407370955161ULL and 1144644202170355111ULL as literals.
    – etarion
    Mar 10, 2011 at 11:00
  • Nim, no warnings when compiling, and -Wall is set
    – Frederik
    Mar 10, 2011 at 11:06
  • etarion, I get a warning if those numbers are 1 digit longer and need to use ULL to get rid of the warning. Either way, the same occurs.
    – Frederik
    Mar 10, 2011 at 11:07
  • You simply can't reliably translate a CPU guarantee into a C/C++ guarantee because you don't know what the compiler will do. Jun 26, 2017 at 0:22

3 Answers 3

12

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:

3
  • Interesting! Thanks for taking the time to dissasemble that!
    – Frederik
    Mar 10, 2011 at 11:25
  • What compiler version, platform and compiler options are you using? That makes an awful huge difference. 64 bit writes will only be atomic if the object is 8 byte aligned and the system is running 64 bit code, if running in 32bit mode (OS is 32bit, or OS is 64, but binary is 32) the writes will not be atomic Mar 10, 2011 at 11:37
  • 1
    GCC 4.2, MacOS X, CPU core i7, OS is 64-bit, code is compiled for x86_64 architecture. The write of 64-bit value is atomic, but 64-bit immediate values cannot be represented in opcode as @AProgrammer pointed. So the compiler must either copy the immediate value to register before moving it to memory, or it must store the value non atomically copying the two 32-bit half of the immediate value. Mar 10, 2011 at 11:41
12

http://www.x86-64.org/documentation/assembly.html

immediate values inside instructions remain 32 bits.

There is no way for the compiler to do the assignation of a 64 bits constant atomically, excepted by first filling a register and then moving that register to the variable. That is probably more costly than assigning directly to the variable and as atomicity is not required by the language, the atomic solution is not chosen.

2
  • Another great reply! Thank you!
    – Frederik
    Mar 10, 2011 at 11:30
  • If there were mov immediate instructions with both an 8 byte value and and an 8 byte address, the resulting instruction size would be just terrible. Can see why they didn't want to build a decoder for that!
    – Bo Persson
    Mar 10, 2011 at 17:34
3

The Intel CPU documentation is right, aligned 8 Bytes read/writes are always atomic on recent hardware (even on 32 bit operating systems).

What you don't tell us, are you using a 64 bit hardware on a 32 bit system? If so, the 8 byte write will most likely be splitted into two 4 byte writes by the compiler.

Just have a look at the relevant section in the object code.

1
  • Hi drhirsch, The system is 64 bit linux. uname output is: Linux acorn 2.6.35-25-server #44 SMP Fri Feb 11 15:50:10 GMT 2011 x86_64 GNU/Linux
    – Frederik
    Mar 10, 2011 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.