473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

atomic increment

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

with lock:
old = atomic_int
atomic_int += 1

but in one operation
Aug 26 '08 #1
4 6636
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
Aug 26 '08 #2
On Aug 26, 5:56*pm, "Diez B. Roggisch" <de...@nospam.web.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
Aug 27 '08 #3
Frank Millman <fr***@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(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.
Aug 27 '08 #4
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-o...hread-safe.htm

and some additional discussion here:

http://mail.python.org/pipermail/pyt...ead.html#69981

</F>

Aug 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ben Young | last post by:
Hi there, I am having a problem with the following code: CREATE TABLE temp ( num integer UNIQUE ); INSERT INTO temp (num) VALUES (1); INSERT INTO temp (num) VALUES (2); INSERT INTO temp (num)...
3
by: Ole Nielsby | last post by:
I need to implement reference counting in a class hierarch, in a thread safe manner. (The classes are umanaged but I might want to compile them with the /clr option.) Some of the objects - atoms...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
11
by: Jon Harrop | last post by:
Can read locks on a data structure be removed safely when updates are limited to replacing a reference? In other words, is setting a reference an atomic operation? I have been assuming that all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.