Scheme 48 Manual | Contents | In Chapter: Threads
Previous: Condition variables | Next: Condition variables

## Condition variables

Condition variables (defined in the `condvars` structure) allow threads perform condition synchronization: It allows threads to block, waiting for a specified condition--associated with a condition variable--to occur, and other threads to wake up the waiting threads when the condition is fulfilled.

Note that, in Scheme 48, condition variables work in conjunction with proposals, not with mutex locks or semaphores, as in most other implementations of this concept.

- `(make-condvar) -> condvar`
- `(make-condvar id) -> condvar`
- `(condvar? thing) -> boolean`
- `(set-condvar-has-value?! condvar boolean)`
- `(condvar-has-value? condvar) -> boolean`
- `(set-condvar-value! condvar value)`
- `(condvar-value condvar) -> value`
- `(maybe-commit-and-wait-for-condvar condvar) -> boolean`
- `(maybe-commit-and-set-condvar! condvar value) -> boolean`

`Make-condvar` creates a condition variable. (The optional `id` argument is only for debugging purposes; the discloser for condition variables prints it out if present.) `Condvar?` is the predicate for condition variables.

Each condition variable has an associated value and a flag `has-value?` signalling if the condition has already occured. The accessor for flag is `condvar-has-value?`; `set-condvar-has-value?!` sets it. Both are provisional operations and go through the current proposal. `Set-condvar-value!` sets the value of the condition variable (provisionally), and `condvar-value` extracts it.

`Maybe-commit-and-wait-for-condvar` attempts to commit the current proposal. If the commit succeeds, it suspends the current thread and registers it with the `condvar` condition variable. Upon waking up again `maybe-commit-and-wait-for-condvar` returns `#t`, If the commit fails, `maybe-commit-and-set-condvar` returns `#f`.

`Maybe-commit-and-set-condvar!` sets the value of the `condvar` condition variable to `value`, (provisionally) sets the `has-value?` flag to `#t`, and then attempt to commit the current proposal. Upon success, it wakes up all suspended threads registered with `condvar` and returns `#t`, otherwise, it returns `#f`.

Previous: Condition variables | Next: Condition variables