@@ -8,14 +8,17 @@ is perfect and all of our problems are solved.
8
8
9
9
Everything is terrible and we have new and exotic problems to try to solve.
10
10
11
- Many people like to believe that Rust eliminates resource leaks, but this is
12
- absolutely not the case, no matter how you look at it. In the strictest sense,
13
- "leaking" is so abstract as to be unpreventable. It's quite trivial to
14
- initialize a collection at the start of a program, fill it with tons of objects
15
- with destructors, and then enter an infinite event loop that never refers to it.
16
- The collection will sit around uselessly, holding on to its precious resources
17
- until the program terminates (at which point all those resources would have been
18
- reclaimed by the OS anyway).
11
+ Many people like to believe that Rust eliminates resource leaks. In practice,
12
+ this is basically true. You would be surprised to see a Safe Rust program
13
+ leak resources in an uncontrolled way.
14
+
15
+ However from a theoretical perspective this is absolutely not the case, no
16
+ matter how you look at it. In the strictest sense, "leaking" is so abstract as
17
+ to be unpreventable. It's quite trivial to initialize a collection at the start
18
+ of a program, fill it with tons of objects with destructors, and then enter an
19
+ infinite event loop that never refers to it. The collection will sit around
20
+ uselessly, holding on to its precious resources until the program terminates (at
21
+ which point all those resources would have been reclaimed by the OS anyway).
19
22
20
23
We may consider a more restricted form of leak: failing to drop a value that is
21
24
unreachable. Rust also doesn't prevent this. In fact Rust has a * function for
@@ -181,7 +184,26 @@ in memory.
181
184
## thread::scoped::JoinGuard
182
185
183
186
The thread::scoped API intends to allow threads to be spawned that reference
184
- data on the stack without any synchronization over that data. Usage looked like:
187
+ data on their parent's stack without any synchronization over that data by
188
+ ensuring the parent joins the thread before any of the shared data goes out
189
+ of scope.
190
+
191
+ ``` rust
192
+ pub fn scoped <'a , F >(f : F ) -> JoinGuard <'a >
193
+ where F : FnOnce () + Send + 'a
194
+ ```
195
+
196
+ Here `f ` is some closure for the other thread to execute . Saying that
197
+ `F : Send + 'a ` is saying that it closes over data that lives for `'a `, and it
198
+ either owns that data or the data was Sync (implying `& data ` is Send ).
199
+
200
+ Because JoinGuard has a lifetime , it keeps all the data it closes over
201
+ borrowed in the parent thread . This means the JoinGuard can 't outlive
202
+ the data that the other thread is working on . When the JoinGuard * does * get
203
+ dropped it blocks the parent thread , ensuring the child terminates before any
204
+ of the closed - over data goes out of scope in the parent .
205
+
206
+ Usage looked like :
185
207
186
208
```rust ,ignore
187
209
let mut data = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ];
0 commit comments