Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in Python only
Search
Advanced Search
Forums
BYTES
Product Launch
Updates
Developer Toolkit
Today's Posts
Member List
Calendar
Home
Forum
Topic
Python
atomic increment
Collapse
This topic is closed.
X
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
Alexandru Mosoi
#1
atomic increment
Aug 26 '08, 03:25 PM
how can i do an atomic read+increment? something like
with lock:
old = atomic_int
atomic_int += 1
but in one operation
Diez B. Roggisch
#2
Aug 26 '08, 04:05 PM
Re: atomic increment
Alexandru Mosoi wrote:
how can i do an atomic read+increment? something like
>
with lock:
old = atomic_int
atomic_int += 1
>
but in one operation
As above - the lock (under the assumption that it is actually a
threading.Lock) will ensure that.
Diez
Comment
Post
Cancel
Frank Millman
#3
Aug 27 '08, 05:15 AM
Re: atomic increment
On Aug 26, 5:56 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Alexandru Mosoi wrote:
how can i do an atomic read+increment? something like
>
with lock:
old = atomic_int
atomic_int += 1
>
but in one operation
>
As above - the lock (under the assumption that it is actually a
threading.Lock) will ensure that.
>
Diez
Just out of interest, would the following, without a lock, be safe?
old, atomic_int = atomic_int, atomic_int+1
Frank Millman
Comment
Post
Cancel
Paul Rubin
#4
Aug 27 '08, 05:45 AM
Re: atomic increment
Frank Millman <frank@chagford .comwrites:
Just out of interest, would the following, without a lock, be safe?
old, atomic_int = atomic_int, atomic_int+1
No I don't think so. But I'm told that in CPython, you can say
counter = iter(xrange(100 00000)) # some number that exceeds what you'll use
...
atomic_int = counter.next()
and the GIL keeps it safe. When in doubt, use a lock or communicate
with other threads through Queues.
Comment
Post
Cancel
Fredrik Lundh
#5
Aug 27 '08, 10:05 AM
Re: atomic increment
Frank Millman wrote:
Just out of interest, would the following, without a lock, be safe?
>
old, atomic_int = atomic_int, atomic_int+1
nope.
there's some information here (make sure you read the comments):
http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm
and some additional discussion here:
429 Too Many Requests
http://mail.python.org/pipermail/python-dev/2006-November/thread.html#69981
</F>
Comment
Post
Cancel
Previous
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment