mirror of
https://github.com/rust-lang/rustlings
synced 2024-11-16 12:12:54 +00:00
Update dead dining philosophers link to the 1.4.0 book example.
This commit is contained in:
parent
72ec3211a0
commit
95669896fa
@ -118,7 +118,7 @@ Check out the [Iterators chapter of the book](https://doc.rust-lang.org/stable/b
|
||||
|
||||
### Threads
|
||||
|
||||
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.
|
||||
See [the Dining Philosophers example](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html) and the [Concurrency Chapter](https://doc.rust-lang.org/stable/book/concurrency.html) from the book.
|
||||
|
||||
- ["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>)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user