Skip to content

Commit 4bff63f

Browse files
authored
Rollup merge of rust-lang#58893 - benaryorg:thread_local_example_join, r=alexcrichton
race condition in thread local storage example The example had a potential race condition that would still pass the test. If the thread which was supposed to modify it's own thread local was slower than the instruction to modify in the main thread, then the test would pass even in case of a failure. This is would be minor if the child thread was waited for since it check using an `assert_eq` for the same thing, but vice versa. However, if the `assert_eq` failed this would trigger a panic, which is not at all caught by the example since the thread is not waited on. Signed-off-by: benaryorg <binary@benary.org>
2 parents 9c99511 + 2293d22 commit 4bff63f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/libstd/thread/local.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ use crate::mem;
4040
/// });
4141
///
4242
/// // each thread starts out with the initial value of 1
43-
/// thread::spawn(move|| {
43+
/// let t = thread::spawn(move|| {
4444
/// FOO.with(|f| {
4545
/// assert_eq!(*f.borrow(), 1);
4646
/// *f.borrow_mut() = 3;
4747
/// });
4848
/// });
4949
///
50+
/// // wait for the thread to complete and bail out on panic
51+
/// t.join().unwrap();
52+
///
5053
/// // we retain our original value of 2 despite the child thread
5154
/// FOO.with(|f| {
5255
/// assert_eq!(*f.borrow(), 2);

0 commit comments

Comments
 (0)