Uh oh!
There was an error while loading. Please reload this page.
Catch thread in example - #33668
Conversation
rust-highfive
commented
May 16, 2016
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
brson
commented
May 17, 2016
The example looks correct as written to me. Why join it? |
There was a problem hiding this comment.
This line will get a "must use Result" warning -- if the idea is to put good code in examples, it should be handled.
steveklabnik
commented
May 17, 2016
I agree with @brson here: unless you're specifically doing something with the |
dns2utf8
commented
May 17, 2016
I had the situation, where the inner thread outlived the outer and caused unexpeced behavior: Run on play use std::sync::Arc;use std::sync::atomic::{AtomicUsize,Ordering};use std::thread;use std::time::Duration;fnmain(){let spinlock = Arc::new(AtomicUsize::new(1));let spinlock_clone = spinlock.clone();let thread = thread::spawn(move|| {
spinlock_clone.store(0,Ordering::SeqCst);
thread::sleep(Duration::from_secs(2));for i in0..10{println!("{}", i);}});// Wait for the other thread to release the lockwhile spinlock.load(Ordering::SeqCst) != 0{}// thread.join().is_ok();println!("END");}I could also add a hint to the threading model of rust, but I could not find a reference explaining what happens in this case with the inner thread. |
durka
commented
May 17, 2016
Yeah, the main thread exits as soon as the inner thread releases the lock (and the program ends when the main thread exits) so the numbers are never printed. If the As for using the Result, calling |
steveklabnik
commented
May 17, 2016
dns2utf8
commented
May 19, 2016
@durka you are right about I am thinking about a link to |
dns2utf8
commented
May 19, 2016
Hm, I seem to have broken the build with the link. Do you have an idea? |
There was a problem hiding this comment.
this line has a trailing whitespace, that's what's failing the build
- Consume result of thread join() - Add link to threading model
dns2utf8
commented
May 19, 2016
Thanks @steveklabnik, I rebased it to current master. |
steveklabnik
commented
May 19, 2016
@bors: r+ rollup |
bors
commented
May 19, 2016
📌 Commit 22615ad has been approved by |
Catch thread in example Since this is an example, the code will be copied by many people and should be over correct. ?r @steveklabnik
Since this is an example, the code will be copied by many people and should be over correct.
?r @steveklabnik