java - How to understand spin lock and mutex lock?
给我你的怀抱
给我你的怀抱 2017-06-14 10:51:20
0
3
882

I’ve read a lot of articles online and I’m still confused. Can anyone explain these two concepts to me in an easy-to-understand way?

In python multi-thread coding, I usually use a while True infinite loop in the run method of the thread, then call queue.task_done at the end of the loop body of the infinite loop to remove the queue, and then call join of the queue on the main thread. The method blocks the main thread to prevent the main thread from ending directly. Is this multi-threaded coding method reasonable? Will there be any bugs? In addition, I would like to ask if the infinite loop I call in run is called a spin lock?

给我你的怀抱
给我你的怀抱

reply all(3)
小葫芦

First of all, you must understand what a mutex lock is and what it means.
It is when two threads A and B access the same memory.
Ideally, our execution order should be that after A is completely executed, B will be executed. However, the execution will occupy the CPU instruction time. If no mechanism is used, when A is halfway executed, B will occupy the CPU, and B will be executed. To process this memory, then B completes execution, and A gets the CPU again, wouldn’t the memory data be wrong?
For memory data security. A mutually exclusive technology is used. When A accesses this memory, it first determines whether this memory has a flag in use (named a lock). If not, it adds a flag (lock) to this memory. , then A is processing this memory, and A unlocks it after processing. If B comes to access while A is processing the memory, B will see that this memory has an in-use mark (lock), and B can have several behaviors. Behavior 1: Taking up CPU. Continuously loop and test the status of the lock. The thread will not hang (sleep) and is in a busy waiting state. A lock that adopts this behavior is called a spin lock. Behavior 2: Thread B sleeps and blocks, giving up the CPU until A finishes executing and the lock is gone, then uses memory. This behavior is called a mutex lock. After seeing this, you probably understand that locks are synchronization mechanisms that implement mutual exclusion. A spin lock is just a case of a mutex lock (it will occupy the CPU's mutex lock while waiting).
Don’t be misled by the name. To understand the mechanism behind it, you have to understand it even if you change the name,
Reference link Link description

过去多啦不再A梦

1. When using a while loop in the Python multi-threaded run method, if the stop program mechanism is not used in the loop body, it will continue to run. Therefore, if the poster wants to code properly, he can use a semaphore or other variable mechanism to notify the loop body. Stop, or determine whether the queue is empty. If it is empty, break directly and exit the loop.

2. The infinite loop in run is not a spin lock. If there is resource competition within the loop, a lock is added, but this lock is also a mutual exclusion lock.
Python’s lock uses the semaphore, not spinlock.

// https://svn.python.org/projects/python/trunk/Python/thread_atheos.h
static int fastmutex_lock(fastmutex_t * mutex)
{
    atomic_t prev = atomic_add(&mutex->count, 1);
    if (prev > 0)
        return lock_semaphore(mutex->sem);
    return 0;
}

Spin lock: Multiple threads access the same resource at the same time. It is a lock set to prevent inconsistent reading and modification of resources. If a thread already occupies the resource when a thread accesses the resource, then the latter thread will wait for the current thread to release the resource. At this time, the latter (without sleeping) keeps running the CPU to detect whether the resources occupied by the former are released. The mechanism by which the latter accesses and continuously detects resource occupation is the spin lock.

Mutex lock: The purpose is the same as the spin lock, but the mechanism is different. When the thread occupies the resource, a lock is added. When the latter thread accesses the resource, it enters the sleep state because the resource is occupied, and waits for the resource to be released. Finally, the waiting threads are notified through the semaphore.

过去多啦不再A梦

Python code will run according to this process,

  1. Set up GIL

  2. Switch to a certain thread

  3. Run

  4. The thread exits and is set to sleep state

  5. Unlock GIL

  6. Repeat the above operations

Maybe because of GIL, I don’t seem to have seen spin locks in Python, and mutex locks are mostly used.

The following is the method I used to write multi-threading, for reference only~

import Queue
from threading import Thread

temp_queue = Queue.Queue()


class Test(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        while temp_queue.empty() is False:
            pass
            # do sth here
            # temp = temp_queue.get()


tasks = []
for i in range(10):
    tasks.append(Test())

for task in tasks:
    task.start()

for task in tasks:
    task.join()

Since it is a queue, Queue().get() in Queue explains Remove and return an item from the queue.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template