► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

► | 11 | Concurrency and Parallelism

11.1 | Threads
11.2 | Synchronization
11.3 | Thread-Local Storage
11.4 | Futures
11.5 | Places
11.6 | Engines

▼ | 11.2 | Synchronization

11.2.1 | Events
11.2.2 | Channels
11.2.3 | Semaphores
11.2.4 | Buffered Asynchronous Channels

On this page:

channel?
make-channel
channel-get
channel-try-get
channel-put
channel-put-evt
channel-put-evt?

top ← prev up next →

##### 11.2.2`` Channels

A channel both synchronizes a pair of threads and passes a value from one to the other. Channels are synchronous; both the sender and the receiver must block until the (atomic) transaction is complete. Multiple senders and receivers can access a channel at once, but a single sender and receiver is selected for each transaction.

Channel synchronization is fair: if a thread is blocked on a channel and transaction opportunities for the channel occur infinitely often, then the thread eventually participates in a transaction.

In addition to its use with channel-specific procedures, a channel can be used as a synchronizable event (see Events). A channel is ready for synchronization when channel-get would not block; the channel’s synchronization result is the same as the channel-get result.

For buffered asynchronous channels, see Buffered Asynchronous Channels.

> procedure (channel? v) → boolean?
> v : any/c

Returns #t if v is a channel, #f otherwise.

> procedure (make-channel) → channel?

Creates and returns a new channel. The channel can be used with channel-get, with channel-try-get, or as a synchronizable event (see Events) to receive a value through the channel. The channel can be used with channel-put or through the result of channel-put-evt to send a value through the channel.

> procedure (channel-get ch) → any
> ch : channel?

Blocks until a sender is ready to provide a value through ch. The result is the sent value.

> procedure (channel-try-get ch) → any
> ch : channel?

Receives and returns a value from ch if a sender is immediately ready, otherwise returns #f.

> procedure (channel-put ch v) → void?
> ch : channel?
> v : any/c

Blocks until a receiver is ready to accept the value v through ch.

> procedure (channel-put-evt ch v) → channel-put-evt?
> ch : channel?
> v : any/c

Returns a fresh synchronizable event for use with sync. The event is ready for synchronization when (channel-put ch v) would not block, and the event’s synchronization result is the event itself.

> procedure (channel-put-evt? v) → boolean?
> v : any/c

Returns #t if v is a channel-put event produced by channel-put-evt, #f otherwise.
top ← prev up next →