4

What happens when two threads attempt to lock the same object at the exact same microsecond (or whatever is the smallest length of time a CPU slice or instruction can be measured at)?

Is it even possible for two threads to execute instructions at the exact same concurrent time, or is that an impossibility with today's hardware?

Im working on a project that deals with muitithreading, where any thread may beat the other to the finish line, so to speak. So naturally, the question of "What happens when they all lock at the same time?" has to be addressed IMO.

Martin Bliss
  • 1,043
  • 7
  • 24
  • 1
    try to read this http://www.albahari.com/threading/part2.aspx – Diego D Jul 28 '12 at 16:40
  • 2
    'What happens when they all lock at the same time?' That is not possible by definition. If anything like that can happen, it's not a lock. – Martin James Jul 28 '12 at 16:56
  • 1
    maybe another way to put the question could be: how the lock behaves when multiple threads hit that point? well I guess the inner workings will queue the access to that resource and the only failure scenario would be when you involve several sequential access to different resources and you incur in a so called dead-lock. Anyway the point of lock is to let a full block of code complete before the resource is released for someone else. Otherwise you are not granted and your logic may fail – Diego D Jul 28 '12 at 17:02
  • Diego De Vita correctly interpreted my question. It should be restated "What happens when two threads ATTEMPT to lock the same resource?" – Martin Bliss Jul 28 '12 at 18:09
  • @MartinBliss - one only will succeed. The other will either receive an immediate failure return or will be denied execution until the lock is freed by the thread that succeeded in getting the lock, (or, sometimes, until a timeout interval has passed without getting the lock, resulting in a 'timeout-failed' return). – Martin James Jul 28 '12 at 18:35
  • @MartinJames This seems to contradict sblom's answer below. It sounds as though you are answering the question "How does locking work?" and not what happens when two threads attempt to lock the same resource at the same instant. – Martin Bliss Jul 28 '12 at 19:24
  • Whatever else, I do not contradict @sblom answer. Interprocessor-safe locks require hardware support, the details of which are architecture-dependant. – Martin James Jul 29 '12 at 04:59

2 Answers2

7

This isn't possible, locks couldn't do what they promise. This requires processor support since it is the only one that can ensure that multiple cores don't try to access the same memory location at the same time. An example is this bit of assembly code, used by the x86 version of the CLR in its Monitor.TryEnter() method:

FASTCALL_FUNC CompareExchangeUP,12
        _ASSERT_ALIGNED_4_X86 ecx
        mov     eax, [esp+4]    ; Comparand
        cmpxchg [ecx], edx
        retn    4               ; result in EAX
FASTCALL_ENDFUNC CompareExchangeUP

The cmpxchg processor instruction provides the atomicity guarantee. It is the kind of instruction that any modern core has, the generic name for it is "compare-and-swap". You'll find more about this instruction in this Wikipedia article.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

Modern locks, in pretty much all environments, are designed such that it isn't possible for two threads to lock an object at the same time. It is possible for modern processors to have two threads running on two different cores attempt to take a lock at very nearly the same time, but they all implement synchronization mechanisms that allow software to tell them not to allow it.

For example, x86-64 has MONITOR and MWAIT instructions. They essentially implement, at the microprocessor level, the semantics of .NET's lock(){}, System.Threading.Monitor.Wait() and System.Threading.Monitor.Enter()/.Exit().

sblom
  • 26,911
  • 4
  • 71
  • 95
  • yes but in a .net environment we are talking about the language domain and how the runtime will behave in such a scenario. My answer is: if you don't lock shared resources their access will not be thread safe and your logic may be completely screwed – Diego D Jul 28 '12 at 16:44
  • I like sblom's answer. I figured computer hardware would have some sort of instruction synchronization mechanisms but I wanted to be certain before I make that assumption... Thanks sblom! – Martin Bliss Jul 28 '12 at 18:08