Hi!
I am building a system that consist of multiple threads doing different work. For the purpose of this post, I am simplifying the code.
My system:
using ThreadPools
function work()
(timer) -> begin
stop = isready(running) && fetch(running) isa Stop
if stop
close(timer)
else
println("Hello!")
schedule()
end
end
end
function schedule()
Timer(work(), 2)
end
function manager()
# Schedule first work()
schedule()
# Run till a signal to stop is received
stop = isready(running) && fetch(running) isa Stop
while ! stop
if isready(sig)
take!(sig)
println("Received signal!!!")
end
stop = isready(running) && fetch(running) isa Stop
end
end
function send_signal()
sleep(8)
put!(sig, true)
end
struct Stop end
running = Channel{Stop}(1)
sig = Channel{Bool}(1)
w1 = @tspawnat 2 manager()
errormonitor(w1)
w2 = @tspawnat 3 send_signal()
errormonitor(w2)
sleep(20)
put!(running, Stop())
So we have one thread (w2
) that only put an element in the Channel sig
after 8 seconds. Then we have another thread: w1
, which has two main jobs. First, it has to periodically call work()
. In the example, this happen every other second. The other job it has to do is to check if a signal has come on the Channel sig
.
The system is supposed to run as long as I want it do. In the example, it will run in 20s, before a Stop signal is sent which will stop w1
and the timer.
The problem is that in manager()
the process of waiting for a signal in sig
is blocking the execution of work()
. How can I run this simultaneously, without introducing another thread?