mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-22 00:18:09 -05:00
Moved away from deprecated thread::sleep_ms
Refactored to use thread::sleep and time::Duration and updated link in README.md
This commit is contained in:
parent
89ee19802b
commit
ff94ef9254
3 changed files with 4 additions and 3 deletions
BIN
.README.md.swp
Normal file
BIN
.README.md.swp
Normal file
Binary file not shown.
|
@ -109,7 +109,7 @@ The [Concurrency](https://doc.rust-lang.org/stable/book/concurrency.html) sectio
|
|||
|
||||
See [the Dining Philosophers example](https://doc.rust-lang.org/stable/book/dining-philosophers.html) and the [Concurrency Chapter](https://doc.rust-lang.org/stable/book/concurrency.html) from the book.
|
||||
|
||||
- ["threads1.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+this+compile%21+Scroll+down+for+hints+%3A%29+The+idea+is+the+thread%0A%2F%2F+spawned+on+line+17+is+completing+jobs+while+the+main+thread+is%0A%2F%2F+monitoring+progress+until+10+jobs+are+completed.+If+you+see+6+lines%0A%2F%2F+of+%22waiting...%22+and+the+program+ends+without+timing+out+the+playground%2C%0A%2F%2F+you%27ve+got+it+%3A%29%0A%0Ause+std%3A%3Async%3A%3AArc%3B%0Ause+std%3A%3Athread%3B%0A%0Astruct+JobStatus+%7B%0A++++jobs_completed%3A+u32%2C%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+status+%3D+Arc%3A%3Anew%28JobStatus+%7B+jobs_completed%3A+0+%7D%29%3B%0A++++let+status_shared+%3D+status.clone%28%29%3B%0A++++thread%3A%3Aspawn%28move+%7C%7C+%7B%0A++++++++for+_+in+0..10+%7B%0A++++++++++++thread%3A%3Asleep_ms%28250%29%3B%0A++++++++++++status_shared.jobs_completed+%2B%3D+1%3B%0A++++++++%7D%0A++++%7D%29%3B%0A++++while+status.jobs_completed+%3C+10+%7B%0A++++++++println%21%28%22waiting...+%22%29%3B%0A++++++++thread%3A%3Asleep_ms%28500%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+%60Arc%60+is+an+Atomic+Reference+Counted+pointer+that+allows+safe%2C+shared+access%0A%2F%2F+to+**immutable**+data.+But+we+want+to+*change*+the+number+of+%60jobs_completed%60%0A%2F%2F+so+we%27ll+need+to+also+use+another+type+that+will+only+allow+one+thread+to%0A%2F%2F+mutate+the+data+at+a+time.+Take+a+look+at+this+section+of+the+book%3A%0A%2F%2F+https%3A%2F%2Fdoc.rust-lang.org%2Fstable%2Fbook%2Fconcurrency.html%23safe-shared-mutable-state%0A%2F%2F+and+keep+scrolling+if+you%27d+like+more+hints+%3A%29%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Do+you+now+have+an+%60Arc%60+%60Mutex%60+%60JobStatus%60+at+the+beginning+of+main%3F+Like%3A%0A%2F%2F+%60let+status+%3D+Arc%3A%3Anew%28Mutex%3A%3Anew%28JobStatus+%7B+jobs_completed%3A+0+%7D%29%29%3B%60%0A%2F%2F+Similar+to+the+code+in+the+example+in+the+book+that+happens+after+the+text%0A%2F%2F+that+says+%22We+can+use+Arc%3CT%3E+to+fix+this.%22.+If+not%2C+give+that+a+try%21+If+you%0A%2F%2F+do+and+would+like+more+hints%2C+keep+scrolling%21%21%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Make+sure+neither+of+your+threads+are+holding+onto+the+lock+of+the+mutex%0A%2F%2F+while+they+are+sleeping%2C+since+this+will+prevent+the+other+thread+from%0A%2F%2F+being+allowed+to+get+the+lock.+Locks+are+automatically+released+when%0A%2F%2F+they+go+out+of+scope.%0A%0A%2F%2F+Ok%2C+so%2C+real+talk%2C+this+was+actually+tricky+for+*me*+to+do+too.+And%0A%2F%2F+I+could+see+a+lot+of+different+problems+you+might+run+into%2C+so+at+this%0A%2F%2F+point+I%27m+not+sure+which+one+you%27ve+hit+%3A%29+Please+see+a+few+possible%0A%2F%2F+answers+on+https%3A%2F%2Fgithub.com%2Fcarols10cents%2Frustlings%2Fissues%2F3+--%0A%2F%2F+mine+is+a+little+more+complicated+because+I+decided+I+wanted+to+see%0A%2F%2F+the+number+of+jobs+currently+done+when+I+was+checking+the+status.%0A%0A%2F%2F+Please+open+an+issue+if+you%27re+still+running+into+a+problem+that%0A%2F%2F+these+hints+are+not+helping+you+with%2C+or+if+you%27ve+looked+at+the+sample%0A%2F%2F+answers+and+don%27t+understand+why+they+work+and+yours+doesn%27t.%0A%0A%2F%2F+If+you%27ve+learned+from+the+sample+solutions%2C+I+encourage+you+to+come%0A%2F%2F+back+to+this+exercise+and+try+it+again+in+a+few+days+to+reinforce%0A%2F%2F+what+you%27ve+learned+%3A%29%0A)
|
||||
- ["threads1.rs"](<https://play.rust-lang.org/?code=%2F%2F%20Make%20this%20compile!%20Scroll%20down%20for%20hints%20%3A\)%20The%20idea%20is%20the%20thread%0A%2F%2F%20spawned%20on%20line%2017%20is%20completing%20jobs%20while%20the%20main%20thread%20is%0A%2F%2F%20monitoring%20progress%20until%2010%20jobs%20are%20completed.%20If%20you%20see%206%20lines%0A%2F%2F%20of%20%22waiting...%22%20and%20the%20program%20ends%20without%20timing%20out%20the%20playground%2C%0A%2F%2F%20you%27ve%20got%20it%20%3A\)%0A%0Ause%20std%3A%3Async%3A%3AArc%3B%0Ause%20std%3A%3Athread%3B%0Ause%20std%3A%3Atime%3A%3ADuration%3B%0A%0Astruct%20JobStatus%20%7B%0A%20%20%20%20jobs_completed%3A%20u32%2C%0A%7D%0A%0Afn%20main\(\)%20%7B%0A%20%20%20%20let%20status%20%3D%20Arc%3A%3Anew\(JobStatus%20%7B%20jobs_completed%3A%200%20%7D\)%3B%0A%20%20%20%20let%20status_shared%20%3D%20status.clone\(\)%3B%0A%20%20%20%20thread%3A%3Aspawn\(move%20%7C%7C%20%7B%0A%20%20%20%20%20%20%20%20for%20_%20in%200..10%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20thread%3A%3Asleep\(Duration%3A%3Afrom_millis\(250\)\)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20status_shared.jobs_completed%20%2B%3D%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D\)%3B%0A%20%20%20%20while%20status.jobs_completed%20%3C%2010%20%7B%0A%20%20%20%20%20%20%20%20println!\(%22waiting...%20%22\)%3B%0A%20%20%20%20%20%20%20%20thread%3A%3Asleep\(Duration%3A%3Afrom_millis\(500\)\)%3B%0A%20%20%20%20%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F%20%60Arc%60%20is%20an%20Atomic%20Reference%20Counted%20pointer%20that%20allows%20safe%2C%20shared%20access%0A%2F%2F%20to%20**immutable**%20data.%20But%20we%20want%20to%20*change*%20the%20number%20of%20%60jobs_completed%60%0A%2F%2F%20so%20we%27ll%20need%20to%20also%20use%20another%20type%20that%20will%20only%20allow%20one%20thread%20to%0A%2F%2F%20mutate%20the%20data%20at%20a%20time.%20Take%20a%20look%20at%20this%20section%20of%20the%20book%3A%0A%2F%2F%20https%3A%2F%2Fdoc.rust-lang.org%2Fstable%2Fbook%2Fconcurrency.html%23safe-shared-mutable-state%0A%2F%2F%20and%20keep%20scrolling%20if%20you%27d%20like%20more%20hints%20%3A\)%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F%20Do%20you%20now%20have%20an%20%60Arc%60%20%60Mutex%60%20%60JobStatus%60%20at%20the%20beginning%20of%20main%3F%20Like%3A%0A%2F%2F%20%60let%20status%20%3D%20Arc%3A%3Anew\(Mutex%3A%3Anew\(JobStatus%20%7B%20jobs_completed%3A%200%20%7D\)\)%3B%60%0A%2F%2F%20Similar%20to%20the%20code%20in%20the%20example%20in%20the%20book%20that%20happens%20after%20the%20text%0A%2F%2F%20that%20says%20%22We%20can%20use%20Arc%3CT%3E%20to%20fix%20this.%22.%20If%20not%2C%20give%20that%20a%20try!%20If%20you%0A%2F%2F%20do%20and%20would%20like%20more%20hints%2C%20keep%20scrolling!!%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F%20Make%20sure%20neither%20of%20your%20threads%20are%20holding%20onto%20the%20lock%20of%20the%20mutex%0A%2F%2F%20while%20they%20are%20sleeping%2C%20since%20this%20will%20prevent%20the%20other%20thread%20from%0A%2F%2F%20being%20allowed%20to%20get%20the%20lock.%20Locks%20are%20automatically%20released%20when%0A%2F%2F%20they%20go%20out%20of%20scope.%0A%0A%2F%2F%20Ok%2C%20so%2C%20real%20talk%2C%20this%20was%20actually%20tricky%20for%20*me*%20to%20do%20too.%20And%0A%2F%2F%20I%20could%20see%20a%20lot%20of%20different%20problems%20you%20might%20run%20into%2C%20so%20at%20this%0A%2F%2F%20point%20I%27m%20not%20sure%20which%20one%20you%27ve%20hit%20%3A\)%20Please%20see%20a%20few%20possible%0A%2F%2F%20answers%20on%20https%3A%2F%2Fgithub.com%2Fcarols10cents%2Frustlings%2Fissues%2F3%20--%0A%2F%2F%20mine%20is%20a%20little%20more%20complicated%20because%20I%20decided%20I%20wanted%20to%20see%0A%2F%2F%20the%20number%20of%20jobs%20currently%20done%20when%20I%20was%20checking%20the%20status.%0A%0A%2F%2F%20Please%20open%20an%20issue%20if%20you%27re%20still%20running%20into%20a%20problem%20that%0A%2F%2F%20these%20hints%20are%20not%20helping%20you%20with%2C%20or%20if%20you%27ve%20looked%20at%20the%20sample%0A%2F%2F%20answers%20and%20don%27t%20understand%20why%20they%20work%20and%20yours%20doesn%27t.%0A%0A%2F%2F%20If%20you%27ve%20learned%20from%20the%20sample%20solutions%2C%20I%20encourage%20you%20to%20come%0A%2F%2F%20back%20to%20this%20exercise%20and%20try%20it%20again%20in%20a%20few%20days%20to%20reinforce%0A%2F%2F%20what%20you%27ve%20learned%20%3A\)%0A&version=stable>)
|
||||
|
||||
### Uncategorized
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
struct JobStatus {
|
||||
jobs_completed: u32,
|
||||
|
@ -16,13 +17,13 @@ fn main() {
|
|||
let status_shared = status.clone();
|
||||
thread::spawn(move || {
|
||||
for _ in 0..10 {
|
||||
thread::sleep_ms(250);
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
status_shared.jobs_completed += 1;
|
||||
}
|
||||
});
|
||||
while status.jobs_completed < 10 {
|
||||
println!("waiting... ");
|
||||
thread::sleep_ms(500);
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue