Channel Synchronization in Racket
Our example demonstrates how to synchronize execution across different threads in Racket. We’ll use channels to wait for a thread to finish its work. When waiting for multiple threads to finish, you might prefer to use a semaphore or a condition variable.
#lang racket
(require racket/async-channel)
; This is the function we'll run in a separate thread. The
; 'done' channel will be used to notify another thread
; that this function's work is done.
(define (worker done-channel)
(display "working...")
(sleep 1)
(displayln "done")
; Send a value to notify that we're done.
(async-channel-put done-channel #t))
(define (main)
; Create a channel for synchronization
(define done (make-async-channel))
; Start a worker thread, giving it the channel to
; notify on.
(thread (lambda () (worker done)))
; Block until we receive a notification from the
; worker on the channel.
(async-channel-get done))
(main)To run the program:
$ racket channel-synchronization.rkt
working...doneIf you removed the (async-channel-get done) line from this program, the program would exit before the worker even started.
In this Racket version:
- We use
racket/async-channelto create asynchronous channels, which are similar to Go’s channels. - The
workerfunction is defined to perform some work and then send a notification on the channel. - In the
mainfunction, we create an asynchronous channel, start a new thread running theworkerfunction, and then wait for the notification from the worker. - We use
threadto start a new thread, which is analogous to Go’s goroutines. async-channel-putis used to send a value on the channel, andasync-channel-getis used to receive a value, blocking until one is available.
This example demonstrates how Racket can be used for concurrent programming, using threads and channels for synchronization between them.
Comments powered by Disqus