Skip to content

Instantly share code, notes, and snippets.

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