Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit edcf8da

Browse files
committed
Issue 16998: Clarify that += on a shared value is not atomic.
1 parent 9eefe91 commit edcf8da

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

Doc/library/multiprocessing.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,24 @@ inherited by child processes.
10061006
ctypes type or a one character typecode of the kind used by the :mod:`array`
10071007
module. *\*args* is passed on to the constructor for the type.
10081008

1009-
If *lock* is ``True`` (the default) then a new lock object is created to
1010-
synchronize access to the value. If *lock* is a :class:`Lock` or
1011-
:class:`RLock` object then that will be used to synchronize access to the
1012-
value. If *lock* is ``False`` then access to the returned object will not be
1013-
automatically protected by a lock, so it will not necessarily be
1014-
"process-safe".
1009+
If *lock* is ``True`` (the default) then a new recursive lock
1010+
object is created to synchronize access to the value. If *lock* is
1011+
a :class:`Lock` or :class:`RLock` object then that will be used to
1012+
synchronize access to the value. If *lock* is ``False`` then
1013+
access to the returned object will not be automatically protected
1014+
by a lock, so it will not necessarily be "process-safe".
1015+
1016+
Operations like ``+=`` which involve a read and write are not
1017+
atomic. So if, for instance, you want to atomically increment a
1018+
shared value it is insufficient to just do ::
1019+
1020+
counter.value += 1
1021+
1022+
Assuming the associated lock is recursive (which it is by default)
1023+
you can instead do ::
1024+
1025+
with counter.get_lock():
1026+
counter.value += 1
10151027

10161028
Note that *lock* is a keyword-only argument.
10171029

0 commit comments

Comments
 (0)