Skip to content

Commit f1fd52a

Browse files
committed
Add Cyclic Barrier example. (better example provided by @pitr-ch)
1 parent e9b592d commit f1fd52a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/concurrent/atomic/cyclic_barrier.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@ module Concurrent
55

66
# A synchronization aid that allows a set of threads to all wait for each
77
# other to reach a common barrier point.
8+
# @example
9+
# barrier = Concurrent::CyclicBarrier.new(3)
10+
# jobs = Array.new(3) { |i| -> { sleep i; p done: i } }
11+
# process = -> (i) do
12+
# # waiting to start at the same time
13+
# barrier.wait
14+
# # execute job
15+
# jobs[i].call
16+
# # wait for others to finish
17+
# barrier.wait
18+
# end
19+
# threads = 2.times.map do |i|
20+
# Thread.new(i, &process)
21+
# end
22+
#
23+
# # use main as well
24+
# process.call 2
25+
#
26+
# # here we can be sure that all jobs are processed
827
class CyclicBarrier < Synchronization::LockableObject
928

1029
# @!visibility private

0 commit comments

Comments
 (0)