"Whats the big deal" } # status thr.status # => "sleep", "running", 'etc' # other operations thr.kill # should not be used ‑> raise exception thr.exit thr.stop && thr.wakeup thr.pass thr.join 16 16
shared data and try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. 25 25
a # race condition (one thread creates Queue # then another one creates it again) # To fix: move assignment to #initialize @results ||= Queue.new end Threads share an address space: context, variable and AST (code tree). 28 28
Ruby process and process fork has its own GIL 3. MRI releases GIL when thread hits blocking IO (HTTP request, console IO, etc.). Therefore, blocking IO could run in parallel 4. Other implementations (JRuby) don't have GIL 34 34
pending? status == 'pending' end def collect_payment puts "Collecting payment..." self.status = 'paid' end end order = Order.new(100.00, 'pending') 5.times.map do Thread.new do if order.pending? # check order.collect_payment # set end end end.each(&:join) 43 43
parallel execution of critical sections. Make critical sections as small as possible. Deadlock may occur when one thread waiting for a mutex locked by another thread waiting itself for the first one. Use mutex#try_lock 46 46