RE: Something is wrong with my threads

Andy Jones <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
> But you are aware that this is not reliably possible no matter what you attempt, are you?

It's literally not possible in MRI because of the GIL.  Is that what you mean?  Yes, but jRuby doesn't have one, and two threads can genuinely call the same method at the same time.  My test works fine in jRuby, but I need to ensure I've not broken the code in MRI, too, so the test has to run there as well.

Or do you mean that I can't guarantee for certain that two calls will happen at exactly the same time?  Unfortunately true.  That's why I create 50 threads, each calling the method, instead of two.  In the code you attach you do exactly the same thing: call the method multiple times in threads hoping to get a collision.

> A mutex guarantees that there are no two threads in the critical section. You do not need to prove that it works that way. > Others have done that for you already.

Well, there goes the entire of TDD...  How do I know I've used a mutex correctly?  That I've synchronised the right bit of code? That I haven't introduced other problems? This is why we test.  Or, why I test, anyway; YMMV.

For the record, I've re-read the docs on Thread and I think you are right: #run doesn't guarantee that execution will switch to the thread.  (That's a hell of a gotcha.) I've modified the test so that it counts the number of threads that _did_ get woke, and uses that to work out what the expected results are.  It seems to work fine now.  Thanks for your help.


Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.