gpt4 book ai didi

ruby - 同步ruby中的线程

转载 作者:行者123 更新时间:2023-12-03 13:20:57 24 4
gpt4 key购买 nike

我希望同时启动多个线程。

10.times do
Thread.new do
sleep rand(5) # Initialize all the stuff
wait_for_all_other_threads # Wait for other threads to initialize their stuff
fire! # Go.
end
end

我将如何实现 wait_for_all_other_threads,以便它们同时都成为 fire!

最佳答案

使用障碍同步:http://rubygems.org/gems/barrier/

调用barrier将导致每个线程阻塞,直到所有线程都调用它为止。

关于ruby - 同步ruby中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12478152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com