repo.or.cz
/
ruby-80x24.org.git
/
blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
history
|
raw
|
HEAD
* 2022-01-18 [ci skip]
[ruby-80x24.org.git]
/
test
/
ruby
/
lbtest.rb
blob
c7822c9e9a2cceabb05ea73bf36940615549cc46
1
# frozen_string_literal: false
2
3
class LocalBarrier
4
def initialize(n)
5
@wait = Thread::Queue.new
6
@done = Thread::Queue.new
7
@keeper = begin_keeper(n)
8
end
9
10
def sync
11
@done.push(true)
12
@wait.pop
13
end
14
15
def join
16
@keeper.join
17
end
18
19
private
20
def begin_keeper(n)
21
Thread.start do
22
n.times do
23
@done.pop
24
end
25
n.times do
26
@wait.push(true)
27
end
28
end
29
end
30
end
31
32
n = 10
33
34
lb = LocalBarrier.new(n)
35
36
(n - 1).times do |i|
37
Thread.start do
38
sleep((rand(n) + 1) / 100.0)
39
print "#{i}: done\n"
40
lb.sync
41
print "#{i}: cont\n"
42
end
43
end
44
45
lb.sync
46
print "#{n-1}: cont\n"
47
# lb.join # [ruby-dev:30653]
48
49
print "exit.\n"