On Tuesday 17 June 2003 04:01, Jack Jansen wrote:
but there's one I'd like to elaborate a little on:
Michel Pelletier said:
I belive the reverse, synchronize will reduce user error and deadlocking code. with explicit locks programmers will forget, or become confused, with when and how to explicitly lock and unlock.
The problem is if one piece of code has synchronise a: ... synchronise b: ... and somewhere else you have synchronise b: ... synchronise a: ...
As you pointed out, the programmer must be aware of this when synchronizing with any mechanism. Would you prefer the manual say "don't do the above" or "don't do the below":
lock1 = thread.allocate_lock() lock2 = thread.allocate_lock()
lock1.acquire() try: lock2.acquire() try: ... finally: lock2.release() finally: lock1.release()
# and somewhere else you have
lock2.acquire() try: lock1.acquire() try: ... finally: lock1.release() finally: lock2.release()
Maybe I did this wrong, but aren't the two (and Greg's "synchronized class") all susceptible to this problem and it's not specificly a failure of the 'synchronize' keyword?
Thanks for your comments Jack, I'm going to add this to the discussion section of the PEP.