From: Robert Klemme Date: 2010-02-16T17:41:48+09:00 Subject: Re: Mutex --0016e6db2d6d2be831047fb3b492 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 2010/2/15 Caleb Clausen : > On 2/15/10, Robert Klemme wrote: >> 2010/2/15 Mido Peace : >>> =A0 =A0 @sem=3DSemaphore.new(0) >> >> IIRC semaphores are typically initialized with a positive value, i.e. >> something larger than 0. =A0If you think about it, init with 0 does not >> make too much sense. > > It depends on what you're using the semaphore for. If it's to be used > for locking, you're right. If it's a signaling semaphore, then 0 for > the init value is exactly what's needed. Right you are. Although I'd say that a condition variable is probably a better tool for that. > Mido, what do you want this for? Mutual exclusion? (But then why not > just use a Mutex?) Signaling? Counting semaphore? > > You might consider using a (sigh) Queue or SizedQueue instead. Even > tho it's kinda silly to use a Queue when all you're ever putting on or > taking off the queue is instances of TrueClass. Hm, in that case I'd prefer a semaphore. > It used to be possible to fake signaling semaphore behavior by > creating a Mutex then immediately locking it. That doesn't work > anymore, tho; the thread that unlocks a Mutex now has to be the same > one that locked it. That is not needed. Please see my test - I could not reproduce the behavior reported. Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --0016e6db2d6d2be831047fb3b492 Content-Type: application/octet-stream; name="sem.rb" Content-Disposition: attachment; filename="sem.rb" Content-Transfer-Encoding: base64 X-Attachment-Id: f_g5qg3mkf0 IyEgcnVieTE5CgpyZXF1aXJlICd0aHJlYWQnCgpUaHJlYWQuYWJvcnRfb25fZXhjZXB0aW9uID0g dHJ1ZQoKZGVmIGxvZyhtc2cpCiAgcHJpbnRmICIlLTMwcCAlc1xuIiwgVGhyZWFkLmN1cnJlbnQs IG1zZwplbmQKCmNsYXNzIFNlbWFwaG9yZQogIGRlZiBpbml0aWFsaXplKHZhbCkKICAgIHJhaXNl IEFyZ3VtZW50RXJyb3IsICJXcm9uZyB2YWx1ZSAlcCIgJSB2YWwgdW5sZXNzIHZhbCA+PSAwCiAg ICBAd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CiAgbG9nICJzaWduYWxsaW5nIgog IHNlbS5yZWxlYXNlCmVuZAoKdGguam9pbgo= --0016e6db2d6d2be831047fb3b492--