Created
July 16, 2019 01:49
-
-
Save eLtronicsVilla/c9b4960ab21c2a8e05372c70ea3be3d0 to your computer and use it in GitHub Desktop.
R lock for synchronization mechanism
This file contains 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