I have what I though would be a simple problem but have been disappointed in julia’s ability regarding it. I have a program that send a bunch of packets over UDP. I want to receive them in julia as follows:
Have a “listener thread” that repeatedly calls recv(...)
to read a new UDP packet and update some data (protected by a lock)
using Base.Threads
using Sockets
socket = UDPSocket()
bind(socket, Sockets.localhost, 49691)
lck = ReentrantLock()
data = Vector{UInt8}()
timestamp = Ref(time())
listener = @spawn begin
println("Thread: $(Threads.threadid())")
while true
newdata = recv(socket)
lock(lck) do
copy!(data, newdata)
timestamp[] = time()
end
end
end
Have a “Main thread” that will access the shared data.
println("Thread: $(Threads.threadid())")
while true
Libc.systemsleep(0.05) # Do stuff
lock(lck) do
if timestamp[] < (time() - 0.2)
@warn "data too old"
else
println("Success")
end
end
end
The problem I have is that despite running julia with 4 threads, the listener-thread decides to run on thread 1, the same as the main thread. Therefore recv is not called while the main task is running.
As far as I can tell there is NO WAY to ensure that these two tasks run on different threads…
Unfortunately, the example I have given above DOES NOT reproduce the problem I have described… hopefully it helps explain my goal though. Can anyone help?