atomic section in code

Diez B. Roggisch deets at nospam.web.de
Fri Sep 5 07:07:28 EDT 2008


Ahmad Humayun schrieb:
> On Sep 5, 1:59 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
>> Ahmad Humayun wrote:
>>> I need to create an atomic section in Python code i.e. there is no
>>> context switch to any other thread during the running of that piece of
>>> code. Would would do the trick?
>> use a lock, and make sure that anyone that needs access to the shared
>> state you're manipulating in that section uses the same lock.
>>
>>      lock = threading.Lock() # or RLock() etc [1]
>>
>>      with lock:
>>          section
>>
>> this only works if everyone honors the lock, of course; there's no way
>> in Python to lock out non-cooperating external threads.
>>
>> </F>
>>
>> 1) seehttp://effbot.org/zone/thread-synchronization.htm
> 
> Thats true, but this will ensure mutual exclusion; not atomicity

AFAIC that kind of mutual exclusion is what atomicity is about. What 
else do you expect to happen?

Diez



More information about the Python-list mailing list