JacquesB 0 Newbie Poster

Hi,

I have a multithreading bug somewhere, probably not in the semaphores, but would like to make sure if out-of-order execution (OoOE) could produce some weird problem:

I explain the problem in 3 steps:

Step 1:

mov eax, dword ptr [Some_global_variable_ONE]
mov ebx, dword ptr [Some_global_variable_TWO]

Due to OoOE, the second instruction can take place before the first. Correct me if I am wrong.

Step 2:

lock dec dword ptr [Some_global_variable_ONE]
jnz xxx
mov ebx, dword ptr [Some_global_variable_TWO]

Due to OoOE, the third instruction can take place before the first when the branch is not taken. Therefore, the "lock dec" is not a valid semaphore mechanism. Correct me if I am wrong.

Step 3:

mov eax, 1
mov edx, 0
lea esi, Some_global_variable_ONE
lock cmpxchg [esi], edx
jnz xxx
mov ebx, dword ptr [Some_global_variable_TWO]

This should be Ok, because I think it is the usual way to do that, but what is the difference with the previous cases? Is Case 3 unsafe as a semaphore?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.