Skip to content

Commit ed175d1

Browse files
committed
09_concurrency lab update
1 parent 3186771 commit ed175d1

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

content/lessons/09_concurrency/index.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
+++
22
title = "Fearless concurrency"
3-
date = 2022-11-28
3+
date = 2024-11-14
44
weight = 1
55
[extra]
6-
lesson_date = 2024-11-21
6+
lesson_date = 2024-11-14
77
+++
88

99
## Parallelism vs Concurrency
@@ -12,16 +12,18 @@ Concurrency is when tasks **can make** progress **independently** of each other.
1212

1313
Parallelism is when multiple tasks **make** progress **at the same time**.
1414

15-
## Concurrency models in Rust
15+
# Concurrency models in Rust
1616

17-
### Threads
17+
## Threads
1818

1919
Nothing unusual here.
2020

2121
Threads can be created with the `thread::spawn` function [docs - please read them!](https://doc.rust-lang.org/std/thread/fn.spawn.html).
2222

2323
This method returns a `JoinHandle<T>` which can be used to wait for the thread to finish. `T` is the type of the thread's return value.
2424

25+
Another way to spawn threads is using [`scope`](https://doc.rust-lang.org/std/thread/fn.scope.html). Threads created in such way are mandatorily joined at the end of the scope, which guarantees that they will borrow items for no longer that the lifetime of the scope. Hence, they can borrow non-`'static` items!
26+
2527
#### Propagating panics
2628

2729
In Rust a panic of one thread doesn't affect the other threads (similar to how Java handles exceptions in threads).
@@ -48,6 +50,32 @@ Normal ownership rules still apply. It means that we cannot mutate the vector in
4850

4951
But what if we need to share some state?
5052

53+
### Send and Sync
54+
55+
They are marker traits used to indicate that a type or a reference to it can be used across threads. See the [nomicon](https://doc.rust-lang.org/nomicon/send-and-sync.html) for more information.
56+
> - A type is `Send` if it is safe to move it (*send* it) to another thread.
57+
> - A type is `Sync` if it is safe to share (*sync*) between threads (`T` is `Sync` if and only if `&T` is `Send`).
58+
59+
This makes sense, because `Sync` is about *sharing* object between threads, and `&` is the *shared* reference.
60+
61+
There is also a great answer on Rust forum, listing + explaining example types that are `!Send` or `!Sync`: https://users.rust-lang.org/t/example-of-a-type-that-is-not-send/59835/3.
62+
63+
For more convenient analysis, examples are listed here:
64+
65+
#### `Send + !Sync`:
66+
- `UnsafeCell` (=> `Cell`, `RefCell`);
67+
68+
#### `!Send + !Sync`:
69+
- `Rc`
70+
- `*const T`, `*mut T` (raw pointers)
71+
72+
#### `!Send + Sync`:
73+
- `MutexGuard` (hint: `!Send` for POSIX reasons)
74+
75+
Exercise for the reader: explain reasons for all limitations of the above types.
76+
77+
## Sharing state between threads
78+
5179
### Message passing
5280

5381
One possible way is to use message passing. We can use a blocking queue (called `mpsc` - ["multi producer single consumer FIFO queue"](https://doc.rust-lang.org/std/sync/mpsc/index.html)) to do it.
@@ -68,11 +96,7 @@ Please read more about them in [the book](https://doc.rust-lang.org/stable/book/
6896

6997
[RwLocks](https://doc.rust-lang.org/std/sync/struct.RwLock.html) are similar to mutexes, but they distinguish between read and write locks.
7098

71-
## Send and Sync
72-
73-
They are marker traits used to indicate that a type or a reference to it can be sent across threads. See the [nomicon](https://doc.rust-lang.org/nomicon/send-and-sync.html) for more information.
74-
75-
## Atomic types
99+
### Atomic types
76100

77101
Atomic types are described in [the docs](https://doc.rust-lang.org/std/sync/atomic/).
78102

@@ -171,7 +195,7 @@ pub fn spawn<F, T>(f: F) -> JoinHandle<T> where
171195

172196
#### Exercise for the reader
173197

174-
Why is it impossible to share a reference to a `Mutex` between threads?
198+
Why is it impossible to share a reference to a `Mutex` between threads spawned with `std::thread::spawn`?
175199

176200
## Data parallelism with Rayon
177201

@@ -187,3 +211,7 @@ Why do that? Because thread synchronization is hard! [Rust prevents data races](
187211

188212
- [The Book](https://doc.rust-lang.org/book/ch16-00-concurrency.html)
189213
- [Safely writing code that isn't thread-safe](http://archive.today/WFlZV)
214+
215+
## No assignment this week
216+
217+
Please work on the first iteration of the big project instead.

content/lessons/10_design_patterns/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title = "Design patterns"
33
date = 2022-12-05
44
weight = 1
55
[extra]
6-
lesson_date = 2024-11-14
6+
lesson_date = 2024-11-21
77
+++
88

99
## Object-oriented programming and Rust

0 commit comments

Comments
 (0)