locks

Jeremy Jones zanesdad at bellsouth.net
Wed Oct 13 07:33:26 EDT 2004


Ajay wrote:

>hi!
>
>what would happen if i try to access a variable locked by another thread? i
>am not trying to obtain a lock on it, just trying to access it.
>
>cheers
>
>
>
>
>
>----------------------------------------------------------------
>This message was sent using IMP, the Internet Messaging Program.
>  
>
I may be wrong, but I'm not sure you can lock a variable.  Actually, you 
probably could do something like "locking a variable" with 
__getattr__(), __setattr(), and __delattr__() and sprinkling gently with 
locks.  The locking mechanisms in the thread and threading libraries 
(allocate_lock() and Lock() respectively) lock sections of code.  When 
you hit a section of code you want locked, you do a <lock>.acquire() and 
when you want to unlock an acquired lock, you do a <lock>.release().  If 
you have a variable in a section of code that you have locked, you can 
still see/smell/taste/touch/modify it from another thread if you can 
access it.  So, if you've passed an object to multiple threads and one 
has acquired a lock to a specific section of code, another thread can't 
enter that section of code until the first releases the lock.  But, if 
you have the same object in a different section of code, you can execute 
that unlocked code that contains/points-to the object.

HTH,

Jeremy Jones



More information about the Python-list mailing list