From: MenTaLguY Date: 2007-01-20T02:48:38+09:00 Subject: Re: [ANN] fastthread 0.6.2 --=-Ps3/XlrAYAbPOl+U0ZKV Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Fri, 2007-01-19 at 13:03 +0900, Tim Pease wrote: > Does fastthread allow you to see who has the lock on the mutex? fastthread is just a re-implementation of the standard API offered by Mutex, ConditionVariable, Queue and SizedQueue in thread.rb. That isn't an ability offered by stdlib's Mutex. > Sync allows you to do this, Not safely, actually. Why do you need to do that? > and it allows me to write re-entrant methods much more easily. Yes, since Sync locks are reentrant, but personally I would avoid Sync because it has some bugs, memory leaks, and the implementation isn't very nice generally. If do you need both a reentrant lock and the ability to see who's currently holding it, what I'd suggest instead is writing your own lock class along these lines: class MyLock def initialize @lock =3D Mutex.new @ready =3D ConditionVariable.new @count =3D 0 end def lock @lock.synchronize do @ready.wait @lock while @owner and @owner !=3D Thread.current @owner =3D Thread.current @count +=3D 1 end self end def unlock @lock.synchronize do return self unless @owner @count -=3D 1 if @count.zero? @owner =3D nil @ready.signal end end self end # safely pass the current holder of the lock to a block def with_locker @lock.synchronize { yield @owner } end end The above code should work fine with or without fastthread. fastthread would just make it a little faster (I'd recommend making it an optional require, as described in the announcement). I'd strongly recommend against writing code which checks the current owner of a lock, by the way, but if you really need to do it (I don't know your specific requirements), something like MyLock#with_locker is probably the safest way to do so. -mental --=-Ps3/XlrAYAbPOl+U0ZKV Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) iD8DBQBFsQRzSuZBmZzm14ERAleZAJ91BDiWaMB7vSNAlqGQTX1EMuz+9wCfUQwZ 4mbffeLrz4egSu1G4FSjbhw= =ke22 -----END PGP SIGNATURE----- --=-Ps3/XlrAYAbPOl+U0ZKV--