Thread lock and other uses

 

A deadlock with recursive lock

Deadlock phenomenon rooted in that the locking function comprises a continuous function, and the respective locking and unlocking the lock there is a certain time lag. When inconsistencies order more of these functions appear locked when style different processes / threads to call these functions, possibly because of a different order / thread releases the lock of the first to grab locked in the process, lead to other process / thread lock also grab inconsistent and can not be used continuously in a performance function, the deadlock.

Advantages: Targeted lock, each lock given different permissions, to facilitate the process control function.

Disadvantages: performance function may be different because the function logic needs, different locking order, but in the thread when calling multiple functions, deadlock will occur, more attention to the point, can not complete the corresponding lock unlock, reduced efficiency and accuracy.

Recursive lock:

In order to solve the deadlock, the introduction of a recursive lock.

Recursive lock symbol: from threading import RLock

lock_A=lock_B=RLock()

General principle is that when initialized, all of the locks treated equally. Excluding the type of lock to unlock the lock only count the number of times. During initialization, the default number is 0 lock, lock function for each time after the count is incremented by 1 each time the lock solution, 1 is subtracted technology. When the lock times to return to zero, indicating that the function can be in multiple processes / threads scramble call stage.

Benefits: all the locks of equal treatment, does not necessarily correspond with the type of lock de-plus, just consider the number of lock. In the process / thread when you call the function, easy to detect and use.

Disadvantages: poor but lock targeted. The lock can not be targeted.

Two, GIL Global Lock

Because the total data is shared by the same thread, in order to ensure security and stability data, python when early development, because it was the only one CPU core, the thread will be locked for the whole, this lock to become GIL global lock. With follow-up technological development, the emergence of multi-core processors, but python global lock preserved (due to change the language of consuming too much), which led to differences affect the following process data. This difference for cpython (python as interpretative language, C language interpreter based on the formation identification byte code) more obvious, and other jpython pypy like are not affected. 

cpython explain path:

Substantially Path: File - compiler (c bytecode language recognition) - Virtual Machine (byte code is converted into machine code) - CPU.

Thus cpython limited, one time only allows a single core thread / process interpreter.

Plus the advantages of global lock: to ensure the security of resources cpython interpreter.

Disadvantages: a single process multi-threaded multi-core resource can not be used, in theory, reduce the efficiency of the use of the CPU.

cpython run different types of blocking the emergence of its own characteristics:

1, I / O obstruction

Concurrently by multiple threads of a single process running speed is not slower than the concurrent multi-process.

The reason single-core CPu in a multithreaded process running encounter with blocking IO, it will switch between multiple threads. CPU to run multiple processes and multiple (concurrent) for each process encounters IO blocked in a wait state, the same will be switched with each other, imagine when all processes in a blocked state, all of the same CPU will at a standstill. In addition, the opening speed faster than the process thread, the thread open resource-intensive to be lower than the process. From the above two points, the decision process of a single thread (single-core) and more complicated process in parallel (multi-core) compared to a bit of advantage. It can be simply understood as the blocking from the outside, even the best computer you must wait for external processing.

Open multiple threads to run faster than the multi-process.

2, compute-intensive blocked

Reason is that a blockage computationally intensive CPU operation does not appear to an end the source codes, only, it will switch the CPU for extended periods of time in a plurality of calculation.

In this process, a single multi-threaded parallel process, the difference is that more complicated process operation for a single-core CPU to be switched between the different threads, a CPU is simultaneously processing a plurality of computing resources among multiple threads to distribution. For the multi-core CPU, each CPU to process a single operation, with no mutual handover, the computer case reasonable CPU resources allocated, for a single CPU will focus on operations. A shorter time. 

3, with the difference GIL global lock lock lock

The same point: are the same kinds of locks, mutex

difference:

1) GIL global lock to protect the internal resource data safe interpreters.

2) GIL lock and unlock without manual.

3)

 

Third, the amount of information 

The amount of information that allows multiple threads / processes were robbed CPU resources.

And the use of symbols: from threading import Semaphore, usage and lock similar.

First initialized, a predetermined single maximum throughput sem = Semaphore (5).

Second, the function being executed inside the lock statement, sem.acquire ()

Finally, after the statement is executed, the unlock sem.release ().

Approximately role is to provide that the maximum number of threads that can run simultaneously, if a larger number of threads need to be addressed, will be similar to the banking window to achieve a vacancy to fill.

Four, socket communication based on multi-threaded

Based on socket and multi-link (dual server while loop) before multi-threaded socket communication based on the similarities and differences:

The former is mainly to solve the problem of how to open a multi-threaded server at the same end.

 

  

 

Guess you like

Origin www.cnblogs.com/fuzhang/p/11402470.html