Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. threads = []
  2. (1..5).each do |i|
  3. threads << Thread.new { `process x#{i}.bin` }
  4. end
  5. threads.each do |t|
  6. t.join
  7. # i'd like to get the output of the process command now.
  8. end
  9.  
  10. threads = []
  11. (1..5).each do |i|
  12. threads << Thread.new { Thread.current[:output] = `echo Hi from thread ##{i}` }
  13. end
  14. threads.each do |t|
  15. t.join
  16. puts t[:output]
  17. end
  18.  
  19. puts t.value
  20.  
  21. #!/usr/bin/env ruby
  22. threads = (1..5).collect do |i|
  23. Thread.new { `echo Hi from thread ##{i}` }
  24. end
  25. threads.each do |t|
  26. puts t.value
  27. end
  28.  
  29. Hi from thread #1
  30. Hi from thread #2
  31. Hi from thread #3
  32. Hi from thread #4
  33. Hi from thread #5
  34.  
  35. a, b, c = [
  36. Thread.new { "something" },
  37. Thread.new { "something else" },
  38. Thread.new { "what?" }
  39. ].map(&:value)
  40.  
  41. a # => "something"
  42. b # => "something else"
  43. c # => "what?"
  44.  
  45. threads = (1..5).collect do |i|
  46. Thread.new { `echo x#{i}.bin` }
  47. end
  48. threads.each do |t|
  49. puts t.value
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement