Guest User

Untitled

a guest
Feb 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. def set(x)
  2. Thread.current[:x] = x
  3. end
  4.  
  5. def say
  6. puts "#{Thread.current.object_id} #{Thread.current[:x].inspect}"
  7. end
  8.  
  9. set 'ok'
  10. say
  11.  
  12. Thread.new do
  13. say
  14. set 't1'
  15. say
  16. end.join
  17.  
  18. class ContextCopyingThread < Thread
  19. def initialize
  20. self[:x] = Thread.current[:x].dup
  21. super
  22. end
  23. end
  24.  
  25. t2 = ContextCopyingThread.new do
  26. say
  27. set 't2'
  28. say
  29. end
  30.  
  31. puts 'waiting...'
  32.  
  33. t2.join
Add Comment
Please, Sign In to add comment