atomic section in code

Ahmad Humayun ahmad.humyn at gmail.com
Fri Sep 5 06:57:37 EDT 2008


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

thanks again,



More information about the Python-list mailing list