2024-08-18 15:23:32 -07:00
|
|
|
# Channels
|
|
|
|
|
|
|
|
Channels are a thread-safe message queue for communicating between threads,
|
|
|
|
although they can also be used as a general-purpose queue.
|
|
|
|
|
2024-08-18 17:00:21 -07:00
|
|
|
## Syntax
|
|
|
|
|
2024-08-18 17:28:16 -07:00
|
|
|
The syntax to create a channel is `|:T|`, where `T` is the type that will be
|
2024-08-18 17:00:21 -07:00
|
|
|
passed through the channel. You can also specify a maximum size for the
|
2024-08-18 20:25:35 -07:00
|
|
|
channel, which will cause giving to block until the recipient has gotten from
|
2024-08-18 17:00:21 -07:00
|
|
|
the channel if the maximum size is reached.
|
|
|
|
|
2024-08-18 15:23:32 -07:00
|
|
|
```tomo
|
2024-08-18 17:28:16 -07:00
|
|
|
channel := |:Int|
|
2024-08-18 20:25:35 -07:00
|
|
|
channel:give(10)
|
|
|
|
channel:give(20)
|
|
|
|
>> channel:get()
|
2024-08-18 15:23:32 -07:00
|
|
|
= 10
|
2024-08-18 20:25:35 -07:00
|
|
|
>> channel:get()
|
2024-08-18 15:23:32 -07:00
|
|
|
= 20
|
2024-08-18 17:00:21 -07:00
|
|
|
|
2024-08-18 17:28:16 -07:00
|
|
|
small_channel := |:Int; max_size=5|
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
## Channel Methods
|
|
|
|
|
2024-08-18 20:25:35 -07:00
|
|
|
### `give`
|
2024-08-18 15:23:32 -07:00
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Adds an item to the channel.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
|
|
|
```tomo
|
|
|
|
func give(channel:|T|, item: T, front: Bool = no -> Void)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `channel`: The channel to which the item will be added.
|
|
|
|
- `item`: The item to add to the channel.
|
2024-09-03 00:53:36 -07:00
|
|
|
- `front`: Whether to put the item at the front of the channel (as opposed to the back).
|
2024-08-18 15:23:32 -07:00
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
Nothing.
|
|
|
|
|
|
|
|
**Example:**
|
2024-10-09 10:48:45 -07:00
|
|
|
```tomo
|
2024-08-18 20:25:35 -07:00
|
|
|
>> channel:give("Hello")
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
2024-08-18 20:25:35 -07:00
|
|
|
### `give_all`
|
2024-08-18 15:23:32 -07:00
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Adds multiple items to the channel.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
|
|
|
```tomo
|
|
|
|
func give_all(channel:|T|, items: [T], front: Bool = no -> Void)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `channel`: The channel to which the items will be added.
|
|
|
|
- `items`: The array of items to add to the channel.
|
2024-09-03 00:53:36 -07:00
|
|
|
- `front`: Whether to put the item at the front of the channel (as opposed to the back).
|
2024-08-18 15:23:32 -07:00
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
Nothing.
|
|
|
|
|
|
|
|
**Example:**
|
2024-10-09 10:48:45 -07:00
|
|
|
```tomo
|
2024-08-18 20:25:35 -07:00
|
|
|
>> channel:give_all([1, 2, 3])
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
2024-08-18 20:25:35 -07:00
|
|
|
### `get`
|
2024-08-18 15:23:32 -07:00
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Removes and returns an item from the channel. If the channel is empty, it waits until an item is available.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
|
|
|
```tomo
|
|
|
|
func get(channel:|T|, front: Bool = yes -> T)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `channel`: The channel from which to remove an item.
|
2024-09-03 00:53:36 -07:00
|
|
|
- `front`: Whether to put the item at the front of the channel (as opposed to the back).
|
2024-08-18 15:23:32 -07:00
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
The item removed from the channel.
|
|
|
|
|
2024-08-18 20:31:36 -07:00
|
|
|
**Example:**
|
2024-10-09 10:48:45 -07:00
|
|
|
```tomo
|
2024-08-18 20:31:36 -07:00
|
|
|
>> channel:peek()
|
|
|
|
= "Hello"
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### `peek`
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Returns the next item that will come out of the channel, but without removing
|
|
|
|
it. If the channel is empty, it waits until an item is available.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
|
|
|
```tomo
|
|
|
|
func peek(channel:|T|, front: Bool = yes -> T)
|
2024-08-18 20:31:36 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `channel`: The channel from which to remove an item.
|
2024-09-03 00:53:36 -07:00
|
|
|
- `front`: Whether to put the item at the front of the channel (as opposed to the back).
|
2024-08-18 20:31:36 -07:00
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
The item removed from the channel.
|
|
|
|
|
2024-08-18 15:23:32 -07:00
|
|
|
**Example:**
|
2024-10-09 10:48:45 -07:00
|
|
|
```tomo
|
2024-08-18 20:25:35 -07:00
|
|
|
>> channel:get()
|
2024-08-18 15:23:32 -07:00
|
|
|
= "Hello"
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### `clear`
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Removes all items from the channel.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
|
|
|
```tomo
|
|
|
|
func clear(channel:|T| -> Void)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `channel`: The mutable reference to the channel.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
Nothing.
|
|
|
|
|
|
|
|
**Example:**
|
2024-10-09 10:48:45 -07:00
|
|
|
```tomo
|
2024-08-18 15:23:32 -07:00
|
|
|
>> channel:clear()
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### `view`
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Returns a list of all items currently in the channel without removing them.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
|
|
|
```tomo
|
|
|
|
func channel:view(->[T])
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `channel`: The channel to view.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
An array of items currently in the channel.
|
|
|
|
|
|
|
|
**Example:**
|
2024-10-09 10:48:45 -07:00
|
|
|
```tomo
|
2024-08-18 15:23:32 -07:00
|
|
|
>> channel:view()
|
|
|
|
= [1, 2, 3]
|
|
|
|
```
|