Sharing a udp socket between threads via parameters

21 views
Skip to first unread message

David Storrs

unread,
Dec 5, 2019, 2:26:31 PM12/5/19
to Racket Users
My understanding is that parameters are copied between threads, but I'm not sure how this interacts with things like file ports, network connections, etc.  Would the following code be problematic?


(define conn (make-parameter))
(define s (udp-open-socket))
(udp-bind! s #f  12345)
(conn s)

(thread (thunk (udp-send-to (conn) "8.8.8.8" 55555 #"hi from bob")))
(thread (thunk (udp-send-to (conn) "8.8.8.8" 55555 #"hi from fred")))

Matthew Flatt

unread,
Dec 5, 2019, 4:11:05 PM12/5/19
to David Storrs, Racket Users
Yes, this works.

As you say, a thread inherits parameter values (at the time that the
thread is created) from its creating thread, and a UDP socket can be
used from multiple threads at once and isn't tied to any particular
thread, so using `(conn)` in different threads is fine.

David Storrs

unread,
Dec 6, 2019, 2:30:24 AM12/6/19
to Matthew Flatt, Racket Users
Cool, thanks! 
Reply all
Reply to author
Forward
0 new messages