atomic operations in presence of multithreading

Discussion in 'Python' started by Glenn Kasten, Jul 27, 2004.

  1. Glenn Kasten

    Glenn Kasten Guest

    I am wondering which operations in Python
    are guaranteed to be atomic in the presence
    of multi-threading. In particular, are assignment
    and reading of a dictionary entry atomic?
    For example, initially:
    dictionary = {}
    dictionary[key] = old_value
    Then thread 1 does:
    v = dictionary[key]
    And thread 2 does:
    dictionary[key] = new_value2
    And thread 3 does:
    dictionary[key] = new_value3
    What I want to make sure of is that
    thread 1 will read either the old_value
    for key, or new_value2, or new_value3,
    but not an intermediate value.
    Which value in particular does not matter to me, as that
    obviously depends on timing. I just want to make sure
    that the dictionary internal data structures can't
    become corrupted by multiple writers / readers.
    Note: I realize that in this particular case
    I can use locks to guarantee safety, but I am
    wondering whether the basic language operations
    such as dictionary reads and updates are guaranteed
    to be atomic.
    A related, more general question, is which
    Python operations are atomic, and which are not.
    I was unable to find this in the language reference,
    but please direct me to the correct location
    if I missed it.
    Thanks,
    Glenn
     
    Glenn Kasten, Jul 27, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glenn Kasten
    Replies:
    8
    Views:
    736
    Dave Brueck
    Jul 29, 2004
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    372
    Jean-Paul Calderone
    Mar 13, 2006
  3. Replies:
    6
    Views:
    10,391
    Walter Roberson
    Mar 26, 2006
  4. Ioannis Vranos

    Multithreading and atomic operations

    Ioannis Vranos, Feb 7, 2009, in forum: C++
    Replies:
    4
    Views:
    1,001
    Ron AF Greve
    Feb 7, 2009
  5. Charles Oliver Nutter

    [ANN] atomic 0.0.1 - An atomic reference for Ruby

    Charles Oliver Nutter, Jun 8, 2010, in forum: Ruby
    Replies:
    5
    Views:
    245
    Robert Dober
    Jun 8, 2010
Loading...

Share This Page