【发布时间】:2018-01-06 14:05:07
【问题描述】:
Crystal lang如何实现模式生产者-消费者?我正在寻找类似的东西 - http://ruby-doc.org/core-2.2.0/Queue.html
可能我需要使用Channel,但我不明白如何......因为它正在等待“消费者”将收到。
我的意思是:
channel = Channel(Int32).new
spawn do
15.times do |i|
# ... do something that take a time
puts "send #{i}"
channel.send i # paused while someone receive, but i want to continue do the job that takes a time..
end
end
spawn do
loop do
i = channel.receive
puts "receive #{i}"
sleep 0.5
end
end
sleep 7.5
【问题讨论】:
标签: multithreading queue crystal-lang