aboutsummaryrefslogtreecommitdiff
path: root/docs/mutexed.md
blob: eebf9015cb7bd0d992f7924eaf122e005dc51b6a (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
# Mutexed Data

To serve the general case of synchronizing access to shared datastructures,
Tomo uses the `mutexed` keyword, which returns a function which can be used
to ensure that all access to a datastructure is guarded by a mutex:

```tomo
with_nums := mutexed [10, 20, 30]

thread := Thread.new(func():
    with_nums(func(nums:&[Int]):
        nums:insert(30)
    )
)

with_nums(func(nums:&[Int]):
    nums:insert(40)
)
    
thread:join()
```

Without having a mutex guard, the code above could run into concurrency issues
leading to data corruption.