Logo

Programming-Idioms

History of Idiom 33 > diff from v16 to v17

Edit summary for version 17 by :
New Python implementation by user [TinyFawks]

Version 16

2016-01-01, 01:38:26

Version 17

2016-02-16, 20:22:05

Idiom #33 Atomically read and update variable

Assign variable x the new value f(x), making sure that no other thread may modify x between the read and the write.

Idiom #33 Atomically read and update variable

Assign variable x the new value f(x), making sure that no other thread may modify x between the read and the write.

Imports
import threading
Code
lock = threading.Lock()

lock.acquire()
try:
	x = f(x)
finally:
	lock.release()
Doc URL
https://docs.python.org/2/library/threading.html#lock-objects
Origin
http://www.laurentluce.com/posts/python-threads-synchronization-locks-rlocks-semaphores-conditions-events-and-queues/