Skip to content

Instantly share code, notes, and snippets.

@zhyq0826
Created September 5, 2016 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhyq0826/4937493628d577cf28604ed0fa7861b5 to your computer and use it in GitHub Desktop.
Save zhyq0826/4937493628d577cf28604ed0fa7861b5 to your computer and use it in GitHub Desktop.
python 多线程模拟竞态
import urllib2
import time
from threading import Thread
#define a global variable
some_var = 0
class IncrementThread(Thread):
def run(self):
#we want to read a global variable
#and then increment it
global some_var
read_value = some_var
print "some_var in %s is %d" % (self.name, read_value)
some_var = read_value + 1
print "some_var in %s after increment is %d" % (self.name, some_var)
def use_increment_thread():
threads = []
for i in range(50):
t = IncrementThread()
threads.append(t)
t.start()
for t in threads:
t.join()
print "After 50 modifications, some_var should have become 50"
print "After 50 modifications, some_var is %d" % (some_var,)
if __name__ == '__main__':
use_increment_thread()
@zhyq0826
Copy link
Author

zhyq0826 commented Sep 5, 2016

Explanation:

  • There is a global variable and all the threads will modify it.
  • All threads should add 1 to the existing value of the variable.
  • There are 50 threads, so at end the value of some_var should become 50, but it doesn't.

Why some_var didn't reach 50?

  • At some point thread t1 read the value of some_var as 15 and then processor took the control from this thread and gave it to thread t2.
  • t2 also reads some_var as 15.
  • Both t1 and t2 reset the value of some_var to 15+1 i.e 16.
  • But when two threads act on some_var we expected it's value to be increased by 2.
  • So, we have a race condition here.
  • A similar race condition might have occurred few more times and so value of some_var at end remains something like 41 or 42 or anything less than 50.

@zhyq0826
Copy link
Author

zhyq0826 commented Sep 5, 2016

import urllib2
import time

from threading import Thread, Lock

#define a global variable
some_var = 0
lock = Lock()

class IncrementThread(Thread):
    def run(self):
        #we want to read a global variable
        #and then increment it
        global some_var
        lock.acquire()
        read_value = some_var
        print "some_var in %s is %d" % (self.name, read_value)
        some_var = read_value + 1 
        print "some_var in %s after increment is %d" % (self.name, some_var)
        lock.release()


def use_increment_thread():
    threads = []
    for i in range(50):
        t = IncrementThread()
        threads.append(t)
        t.start()
    for t in threads:
        t.join()
    print "After 50 modifications, some_var should have become 50"
    print "After 50 modifications, some_var is %d" % (some_var,)


if __name__ == '__main__':
    use_increment_thread()

使用锁来解决多线程带来的资源竞争

  • Lock is used to guard against race condition.
  • If thread t1 has acquired the lock before performing a set of operations, no other thread can perform the same set of operation until t1 releases the lock.
  • We want to make sure that once t1 has read some_var, no other thread can read some_var until t1 is done with modifying the value of some_var.
  • So reading some_var and modifying it are logically related operations here.
  • And that is why we keep read and modify part of some_var guarded by a Lock instance.
  • Lock is a separate object and it will be acquired by the thread from whose context it is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment