Last active
August 13, 2017 03:11
-
-
Save schedutron/5ac091bc2acbe4f67deb4d14e01419d2 to your computer and use it in GitHub Desktop.
A demo script for threading.RLock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
num = 0 | |
lock = Threading.Lock() | |
lock.acquire() | |
num += 1 | |
lock.acquire() # This will block. | |
num += 2 | |
lock.release() | |
# With RLock, that problem doesn’t happen. | |
lock = Threading.RLock() | |
lock.acquire() | |
num += 3 | |
lock.acquire() # This won’t block. | |
num += 4 | |
lock.release() | |
lock.release() # You need to call release once for each call to acquire. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment