12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485(* This file is free software, part of containers. See file "license" for more details. *)(** {1 Threads} *)typet=Thread.tletspawnf=Thread.createf()letspawn1fx=Thread.createfxletspawn2fxy=Thread.create(fun()->fxy)()letdetachf=ignore(Thread.createf())letfinally_fx~h=tryletres=fxinignore(h());reswithe->ignore(h());raiseemoduleArr=structletspawnnf=Array.initn(funi->Thread.createfi)letjoina=Array.iterThread.joinaend(*$R
let l = CCLock.create 0 in
let a = Arr.spawn 101 (fun i -> CCLock.update l ((+) i)) in
Arr.join a;
let n = Iter.(1 -- 100 |> fold (+) 0) in
assert_equal ~printer:CCInt.to_string n (CCLock.get l)
*)moduleBarrier=structtypet={lock:Mutex.t;cond:Condition.t;mutableactivated:bool;}letcreate()={lock=Mutex.create();cond=Condition.create();activated=false;}letwith_lock_bf=Mutex.lockb.lock;finally_f()~h:(fun()->Mutex.unlockb.lock)letresetb=with_lock_b(fun()->b.activated<-false)letwaitb=with_lock_b(fun()->whilenotb.activateddoCondition.waitb.condb.lockdone)letactivateb=with_lock_b(fun()->ifnotb.activatedthen(b.activated<-true;Condition.broadcastb.cond))letactivatedb=with_lock_b(fun()->b.activated)end(*$R
let b = Barrier.create () in
let res = CCLock.create 0 in
let t1 = spawn (fun _ -> Barrier.wait b; CCLock.incr res)
and t2 = spawn (fun _ -> Barrier.wait b; CCLock.incr res) in
Thread.delay 0.2;
assert_equal 0 (CCLock.get res);
Barrier.activate b;
Thread.join t1; Thread.join t2;
assert_equal 2 (CCLock.get res)
*)