atomic increment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandru  Mosoi

    atomic increment

    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
    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

    • Frank Millman

      #3
      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

      • Paul Rubin

        #4
        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

        • Fredrik Lundh

          #5
          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):



          and some additional discussion here:



          </F>

          Comment

          Working...