Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Created January 29, 2013 16:50
Show Gist options
  • Save sw17ch/4665730 to your computer and use it in GitHub Desktop.
Save sw17ch/4665730 to your computer and use it in GitHub Desktop.
require 'thread'
$id = 0
$sem = Mutex.new
def next_id
n = nil
$sem.synchronize do
n = $id
$id += 1
end
return n
end
a = Thread.new do
100.times do
puts "A: #{next_id}"
end
end
b = Thread.new do
100.times do
puts "B: #{next_id}"
end
end
a.run
b.run
a.join
b.join
@sw17ch
Copy link
Author

sw17ch commented Jan 29, 2013

Subsection of af ull run:

B: 1A: 0
B: 2
B: 3
B: 4
B: 5
A: 6

A: 7B: 8

A: 9B: 10

A: 12B: 11

A: 13B: 14
A: 15

B: 16
A: 17B: 18

B: 19A: 20

A: 22B: 21

A: 23
A: 25
B: 24A: 26
A: 27
A: 28

B: 29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment