difference between lock, memory barrier, semaphore

前端 未结 3 1120
甜味超标
甜味超标 2021-01-31 23:34

This article: http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf (page 12) seems to make a difference between a lock and a memory barrier

I would like to know w

相关标签:
3条回答
  • 2021-02-01 00:17

    A memory barrier (also known as a fence) is a hardware operation, which ensures the ordering of different reads and writes to the globally visible store. On a typical modern processor, memory accesses are pipelined, and may occur out of order. A memory barrier ensures that this doesn't happen. A full memory barrier will ensure that all loads and stores which precede it occur before any load or store which follows it. (Many processors have support partial barriers; e.g. on a Sparc, a membar #StoreStore ensures that all stores which occur before it will be visible to all other processes before any store which occurs after it.)

    That's all a memory barrier does. It doesn't block the thread, or anything.

    Mutexes and semaphores are higher level primatives, implemented in the operating system. A thread which requests a mutex lock will block, and have its execution suspended by the OS, until that mutex is free. The kernel code in the OS will contain memory barrier instructions in order to implement a mutex, but it does much more; a memory barrier instruction will suspend the hardware execution (all threads) until the necessary conditions have been met—a microsecond or so at the most, and the entire processor stops for this time. When you try to lock a mutex, and another thread already has it, the OS will suspend your thread (and only your thread—the processor continues to execute other threads) until whoever holds the mutex frees it, which could be seconds, minutes or even days. (Of course, if it's more than a few hundred milliseconds, it's probably a bug.)

    Finally, there's not really much difference between semaphores and mutexes; a mutex can be considered a semaphore with a count of one.

    0 讨论(0)
  • 2021-02-01 00:30

    Simples explanation for now.

    Lock

    Is an atomic test for whether this piece of code can proceed

    lock (myObject)
    {
        // Stuff to do when I acquire the lock
    }
    

    This is normally a single CPU instruction that tests and sets a variable as a single atomic operation. More here, http://en.wikipedia.org/wiki/Test-and-set#Hardware_implementation_of_test-and-set_2

    Memory Barrier

    Is a hint to the processor that it can not execute these instructions out of order. Without it, the instructions could be executed out of order, like in double checked locking, the two null checks could be executed before the lock has.

    Thread.MemoryBarrier();
    public static Singleton Instance()
    {
        if (_singletonInstance == null)
        {
            lock(myObject)
            {
                if (_singletonInstance == null)
                {
                    _singletonInstance = new Singleton();
                }
            }
        }
    }
    

    This are also a set of CPU instructions that implement memory barriers to explicitly tell a CPU it can not execute things out of order.

    Semaphores

    Are similar to locks except they normally are for more than one thread. I.e. if you can handle 10 concurrent disk reads for example, you'd use a semaphore. Depending on the processor this is either its own instruction, or a test and set instruction with load more work around interrupts (for e.g. on ARM).

    0 讨论(0)
  • 2021-02-01 00:38
    • A memory barrier is a method to order memory access. Compilers and CPU's can change this order to optimize, but in multithreaded environments, this can be an issue. The main difference with the others is that threads are not stopped by this.
    • A lock or mutex makes sure that code can only be accessed by 1 thread. Within this section, you can view the environment as singlethreaded, so memory barriers should not be needed.
    • a semaphore is basically a counter that can be increased (v()) or decreased (p()). If the counter is 0, then p() halts the thread until the counter is no longer 0. This is a way to synchronize threads, but I would prefer using mutexes or condition variables (controversial, but that's my opinion). When the initial counter is 1, then the semaphore is called a binary semaphore and it is similar to a lock.

    A big difference between locks and semaphores is that the thread owns the lock, so no other thread should try to unlock, while this is not the case for semaphores.

    0 讨论(0)
提交回复
热议问题