From: Roger Pack Date: 2007-08-29T04:29:14+09:00 Subject: Re: Thread safety techniques for server applications? Thanks for your comments. Forgive me if I called modules/classes statically loaded. What I meant was 'loaded before you start splitting to multiple threads' instead of static. For example I have used xmlrpc before (which relies on REXML) and every so often (note the 'freak chance' aspect) it will throw the exception "REXML::Document not found" despite the fact that it indeed should be, and normally is. I laid the blame on Ruby threads. Where it belongs, I think is on...Ruby threads. But having not actually ever fixed it, I can't say for sure. I believe I've been able to recreate it reliably. Joel VanderWerf wrote: > Roger Pack wrote: > ... >> Also load up all your functions/everything 'single threaded' otherwise >> some of the functions will be 'assigned' to the wrong thread (ugh), then >> be unavailable (classes, modules, too). >> I.e. dynamically declared functions are dangerous. > > In ruby, everything is dynamically defined. The closest you can get to > static definitions is to require all your lib files before starting > threads. Even so, it's not really static. It might be safer in some > cases because require-ing a file is not atomic.(*) That's a corner case, > though. > > I don't think it's possible for things to be unavailable because they > were loaded in the wrong thread, though. Got an example? > >> Note also that sometimes if you are reading from TCPSockets the sockets >> get confused and start reading from one another. > > Really? I've never seen that, even with pretty heavy use of lots of > threads and sockets. Yeah I get it...sometimes a socket that is *only* used for sending will magically 'receive'...its own output! Wow! And other weirdness. Mostly on slower machines. It is odd. I noticed Zed Shaw said he'd run into the same thing (and was unable to track down the cause) in some thread or other here. I honestly don't get it, either, but I think it's half the motivation to the creation of EventMachine. Just my own $0.02 -Roger > > ---- > > (*) An example: > > [~/tmp] cat a.rb > > t = Thread.new do > loop do > sleep 0.1 > puts "a" > end > end > > sleep 0.5 > > require 'b' > > [~/tmp] cat b.rb > t = Thread.new do > loop do > sleep 0.1 > puts " b" > end > end > > sleep 1 > t.kill > > [~/tmp] ruby a.rb > a > a > a > a > a > b > a > b > a > b > a > b > a > b > a > b > a > b > a > b > a > b > a -- Posted via http://www.ruby-forum.com/.