1. Thread base 1.1. thread status
The thread has five states. The state conversion process is shown in:
1.2. Thread Synchronization (LOCK)
the advantage of Multithreading is that it can run multiple tasks at the same time (at least it feels like this ). However, when the thread needs to share data, the data may not be synchronized. Consider this situation: All elements in a list are 0, the thread "set" changes all elements from the back to 1, and the thread "print" reads and prints the list from the back to the back. Then, when the thread "set" starts to change, the thread "print" will print the list, and the output will be half 0 and half 1, which means the data is not synchronized. To avoid this situation, the lock concept is introduced.
There are two locks: Locked and unlocked. Every time a thread such as "set" wants to access Shared data, it must first get a lock. If another thread such as "print" has been locked, it will suspend the thread "set, that is, synchronous blocking. Wait until the thread "print" is accessed, release the lock, and then let the thread "set" continue. After such processing, when the list is printed, either all 0 is output or all 1 is output, and no embarrassing scenes of half 0 and half 1 are displayed.
Shows the interaction between threads and locks:
1.3. Thread communication (condition variable)
However, there is another embarrassing situation: the list does not exist at the beginning, but is created through the thread "CREATE. If "set" or "print" is accessed when "CREATE" is not running, an exception occurs. The lock can solve this problem, but "set" and "print" will require an infinite loop-they do not know when "CREATE" will run, making "CREATE" notify "set" and "print" after running is obviously a better solution. Therefore, conditional variables are introduced.
Conditional variables allow threads such as "set" and "print" to wait when the conditions are not met (when the list is none) and send a notification when the conditions are met (the list has been created, tell "set" and "print" that the conditions already exist. You should get up and work. Then "set" and "print" will continue to run.
Shows the interaction between a thread and a condition variable:
1.4. State Conversion of thread running and blocking
Finally, let's look at the conversion of thread running and blocking states.
there are three blocking conditions:
synchronization blocking refers to a State in which a thread requests a lock to enter this state, once the lock is obtained, it is restored to the running state.
waiting for blocking refers to the status of waiting for notifications from other threads. After the thread obtains the condition lock, the" Waiting "call will enter this status. Once other threads send a notification, the thread enters the synchronization blocking state and the competition condition is locked again.
while other blocking measures call time. sleep (), anotherthread. join () or block when waiting for Io. in this state, the thread will not release the obtained lock.
TIPS: If you can understand the content, the following theme will be very easy; and most of the content is popularProgramming LanguageIs the same. (This means you can't understand it.> _ <if the author's level is low, you should also understand the tutorials for others)
2. Thread
Python supports threads through two standard libraries: thread and threading. Thread provides a low-level, original thread, and a simple lock.
?
| 1234567891011121314151617181920212223242526272829303132333435363738 |