aboutsummaryrefslogtreecommitdiff
path: root/docs/threads.md
blob: 1580df8c182eb201c76c048aa74c0ca45e031766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Threads

Tomo supports POSIX threads (pthreads) through the `Thread` type. The
recommended practice is to have each thread interact with other threads only
through thread-safe Channels with no other shared data.

## Thread Methods

### `new`

**Description:**  
Creates a new thread to execute a specified function.

**Usage:**  
```tomo
Thread.new(fn: func(->Void) -> Thread)
```

**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():
    while yes:
        input := jobs:get()
        results:give(input + 10
)
= Thread<0x12345678>
>> jobs:give(10)
>> results:get()
= 11
```

---

### `cancel`

**Description:**  
Requests the cancellation of a specified thread.

**Usage:**  
```tomo
cancel(thread: Thread)
```

**Parameters:**

- `thread`: The thread to cancel.

**Returns:**  
Nothing.

**Example:**  
```tomo
>> thread:cancel()
```

---

### `join`

**Description:**  
Waits for a specified thread to terminate.

**Usage:**  
```tomo
join(thread: Thread)
```

**Parameters:**

- `thread`: The thread to join.

**Returns:**  
Nothing.

**Example:**  
```tomo
>> thread:join()
```

---

### `detach`

**Description:**  
Detaches a specified thread, allowing it to run independently.

**Usage:**  
```tomo
detach(thread: Thread)
```

**Parameters:**

- `thread`: The thread to detach.

**Returns:**  
Nothing.

**Example:**  
```tomo
>> thread:detach()
```