Thread of that little thing

Source: Internet
Author: User

1. Thread Safety

First we know that threads are shared resources, and on that basis we ask a question if we have a variable a = 0

Then 10 threads are given it +1 and then we execute this 10 thread, and finally a = 10?

Let's look at the following code first

1 __author__='Rico'2 #_*_coding:utf-8_*_3  fromThreadingImportThread4A =05 classPlusOne (Thread):6     def __init__(self):7Super (Plusone,self).__init__()8     defRun (self):9         GlobalaTenA + = 1 One  A  forIinchRange (30): -temp =PlusOne () - Temp.start () the  - PrintA

If the right one should have entered 30.

But---"

The result is 29.

Of course, this is not always the case, is not fixed, look at the configuration of the computer, and the number of threads

So we know that the variables don't get the value we want because the variables are preempted between the threads, causing the problem to occur---> conclusion: Python is not a thread-safe language

So we're going to have to keep our threads safe, so that leads to the following concepts

2-Wire Lock

When we go to buy clothes, will there be two people in the fitting room? Not going to. Why is it? Because everyone goes in, they lock the door and open it when they leave. This is the concept of a thread lock.

Let's modify the Run function

1 def Run (self): 2         Lock.acquire (3)         Global   a4         A + = 15         print  a6         Lock.release ()
Lock.acquire () #在执行这个时候, the thread will monopolize the CPU
Lock.release () #释放
There's a single thread between the two operations.


Python has a thing called the Global interpreter lock, which we all know--the reason for the global interpreter lock when doing CPU operation
You got up a multi-threaded, in fact, a single thread, the other threads are actually slicing
1 __author__='Rico'2 #_*_coding:utf-8_*_3  fromThreadingImportThread,lock4 Import Time5A =06 classPlusOne (Thread):7     def __init__(self,name):8Self.__name=name9Super (Plusone,self).__init__()Ten     defRun (self): One         Globala A Lock.acquire () -A + = 1 - lock.release () the         Print "%s\n"%a -Lock =Lock () -  -  forIinchRange (200): +temp =PlusOne (i) - Temp.start () +  A PrintA

When we run this code, we can feel the slices, no matter how many threads you open, but if you turn the run function into this,

def run (self):        global  a        lock.acquire ()        A + = 1        lock.release ()        time.sleep (1)        print "%s\n "% A

CPU By default All 100 instructions when added to sleep (1) will not wait for the direct cut

3 semaphore also allows a passing thread to access the same data

1 __author__='Rico'2 #_*_coding:utf-8_*_3  fromThreadingImportThread,lock,boundedsemaphore4 Import Time5A =06 classPlusOne (Thread):7     def __init__(self,name):8Self.__name=name9Super (Plusone,self).__init__()Ten     defRun (self): OneTime.sleep (1) A         Globala - Samp.acquire () -A + = 1 the         Print "%s"%a - samp.release () -Samp = Boundedsemaphore (10) -  forIinchRange (200): +temp =PlusOne (i) - Temp.start () +  A PrintA

Thread of that little thing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.