Original Post
(using C++) I've started to get into multithread programming, and I'm wondering if I am properly protecting my data from being corrupted across threads. Until now I've been using the volatile keyword for certain shared variables. But I'm not convinced it helps. say I have a local variable, x, in my wannabe thread-safe function that counts how many time the funcion is called: void dosomething() { volatile static int x = 0; x++; } I'm no expert on reading assembly, but I outputted the function to asm and the x++ part looked like 1 movl _ZZ11dosomethingvE1x, %eax 2 incl %eax 3 movl %eax, _ZZ11dosomethingvE1x In that case, if two concurrent threads call this function, given the nature of multithreading couldn't they be executed in this order?: thread 1: 1 movl _ZZ11dosomethingvE1x, %eax 2 incl %eax thread 2: 1 movl _ZZ11dosomethingvE1x, %eax 2 incl %eax 3 movl %eax, _ZZ11dosomethingvE1x thread 1: 3 movl %eax, _ZZ11dosomethingvE1x So after calling the function twice, might it be that x is only incremented once? I think I may be missing the point of volatile. If so, how can I get around this problem?