liveBook · Manning

putMVar

As explained in Functional Design and Architecture: Examples in Haskell

putMVar is a Haskell operation that places a value into an MVar, blocking while the MVar is full.

putMVar places a value into an MVar. If the MVar is already full, the calling thread blocks until another thread empties it with takeMVar. This thread-safe coordination supports imperative-style mutation within IO. section 8.1.3

A request–response exchange uses putMVar to hand work to another thread:

  • The requester puts a request into the request MVar, then takes the result from the response MVar. section 8.1.3
  • The responder takes the request, processes it, and puts the result into the response MVar. section 8.1.3
  • Every takeMVar should eventually pair with putMVar; branching that skips the write can leave other threads blocked indefinitely. section 8.1.3

Worker response

A worker repeatedly takes a request and publishes the processed value with putMVar response (f a), allowing a separate requester to continue.

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines full and empty states, blocking behavior, FIFO access, and deadlock risks without descending into implementation internals.
Examples
Uses a worker pipe, a FizzBuzz processor, and a generator that sends requests and prints responses.
What it assumes
Assumes familiarity with Haskell IO, threads, MVar, and takeMVar.
What you can do after
Apply putMVar to implement blocking communication between requester and responder threads.
How it connects
Connects MVar with takeMVar, IO, concurrency, and the request–response pattern.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.