RacketConcurrencyParallelismFuturesPlaces

Concurrency and Parallelism in Racket | Schema Programming Part 18

2.66 min read
Md Nasim SheikhMd Nasim Sheikh
Share:

Racket distinguishes clearly between Concurrency (managing multiple tasks) and Parallelism (running tasks simultaneously on multiple cores).

Advertisement

Threads (Concurrency)

Racket threads are lightweight and managed by the VM (green threads). They are great for I/O but don't use multiple cores.

(thread (lambda () 
          (displayln "Hello from thread!")))
(displayln "Hello from main!")

Futures (Fine-grained Parallelism)

For number-crunching on multiple cores, use future.

#lang racket
(require racket/future)

(define f (future (lambda () (loop-heavy-calculation))))
(displayln "Main doing work...")
(touch f) ; Wait for result

Caveat: Futures are limited. If you touch unsafe operations (like I/O), the future essentially blocks.

Places (Coarse-grained Parallelism)

places spawn entirely new instances of the Racket VM. They don't share memory; they communicate via message passing (like Web Workers).

#lang racket
(require racket/place)

(define (worker-entry ch)
  (place-channel-put ch "Work done!"))

(define p (place worker-entry))
(displayln (place-channel-get p))

This guarantees true parallelism across cores but requires careful data marshalling.

Advertisement

Summary

  • Threads: For GUIs, Web Servers, I/O.
  • Futures: For math/tight loops.
  • Places: For heavy, independent tasks.
Quick Quiz

Which construct spawns a separate Racket VM instance to run code in parallel?

Md Nasim Sheikh
Written by

Md Nasim Sheikh

Software Developer at softexForge

Verified Author150+ Projects
Published:

You May Also Like