atomic increment

A

Alexandru Mosoi

how can i do an atomic read+increment? something like

with lock:
old = atomic_int
atomic_int += 1

but in one operation
 
D

Diez B. Roggisch

Alexandru said:
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
 
F

Frank Millman

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
 
P

Paul Rubin

Frank Millman said:
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(10000000)) # 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,838
Latest member
elibuskamoSeAve

Latest Threads

Top