2024-08-18 15:23:32 -07:00
|
|
|
# Threads
|
|
|
|
|
|
|
|
Tomo supports POSIX threads (pthreads) through the `Thread` type. The
|
|
|
|
recommended practice is to have each thread interact with other threads only
|
2025-01-02 13:29:21 -08:00
|
|
|
through [mutex-guarded datastructures](mutexed.md).
|
2024-08-18 15:23:32 -07:00
|
|
|
|
|
|
|
## Thread Methods
|
|
|
|
|
|
|
|
### `new`
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Creates a new thread to execute a specified function.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
2024-08-18 15:23:32 -07:00
|
|
|
```tomo
|
2024-10-09 10:48:45 -07:00
|
|
|
func new(fn: func(->Void) -> Thread)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `fn`: The function to be executed by the new thread.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
A new `Thread` object representing the created thread.
|
|
|
|
|
|
|
|
**Example:**
|
|
|
|
```tomo
|
|
|
|
>> jobs := |Int|
|
|
|
|
>> results := |Int|
|
|
|
|
>> thread := Thread.new(func():
|
2024-11-03 09:20:53 -08:00
|
|
|
repeat:
|
2024-08-18 20:25:35 -07:00
|
|
|
input := jobs:get()
|
|
|
|
results:give(input + 10
|
2024-08-18 15:23:32 -07:00
|
|
|
)
|
|
|
|
= Thread<0x12345678>
|
2024-08-18 20:25:35 -07:00
|
|
|
>> jobs:give(10)
|
|
|
|
>> results:get()
|
2024-08-18 15:23:32 -07:00
|
|
|
= 11
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### `cancel`
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Requests the cancellation of a specified thread.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
2024-08-18 15:23:32 -07:00
|
|
|
```tomo
|
2024-10-09 10:48:45 -07:00
|
|
|
func cancel(thread: Thread)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `thread`: The thread to cancel.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
Nothing.
|
|
|
|
|
|
|
|
**Example:**
|
|
|
|
```tomo
|
|
|
|
>> thread:cancel()
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### `join`
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Waits for a specified thread to terminate.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
2024-08-18 15:23:32 -07:00
|
|
|
```tomo
|
2024-10-09 10:48:45 -07:00
|
|
|
func join(thread: Thread)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `thread`: The thread to join.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
Nothing.
|
|
|
|
|
|
|
|
**Example:**
|
|
|
|
```tomo
|
|
|
|
>> thread:join()
|
|
|
|
```
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### `detach`
|
|
|
|
|
|
|
|
**Description:**
|
|
|
|
Detaches a specified thread, allowing it to run independently.
|
|
|
|
|
2024-10-09 10:48:45 -07:00
|
|
|
**Signature:**
|
2024-08-18 15:23:32 -07:00
|
|
|
```tomo
|
2024-10-09 10:48:45 -07:00
|
|
|
func detach(thread: Thread)
|
2024-08-18 15:23:32 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
- `thread`: The thread to detach.
|
|
|
|
|
|
|
|
**Returns:**
|
|
|
|
Nothing.
|
|
|
|
|
|
|
|
**Example:**
|
|
|
|
```tomo
|
|
|
|
>> thread:detach()
|
|
|
|
```
|