You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is possible to pass threads2 without actually solving a task. Following code will make it great and allow to pass while being wrong. Maybe worth discarding as you can still get it by matching with expected result(10) and checking solution file
use std::{sync::Arc, thread, time::Duration};#[derive(Clone)]structJobStatus{jobs_done:u32,}fnmain(){// TODO: `Arc` isn't enough if you want a **mutable** shared state.let status = Arc::new(JobStatus{jobs_done:0});letmut handles = Vec::new();for _ in0..10{letmut status_shared = Arc::clone(&status);let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250));// TODO: You must take an action before you update a shared value.Arc::make_mut(&mut status_shared).jobs_done += 1;});
handles.push(handle);}// Waiting for all jobs to complete.for handle in handles {
handle.join().unwrap();}// TODO: Print the value of `JobStatus.jobs_done`.println!("Jobs done: {}", status.jobs_done);}
I think there are too many ways to pass the task. Here is another (most likely "wrong") solution:
fnmain(){let status = Arc::new(Mutex::new(JobStatus{jobs_done:0}));letmut handles = Vec::new();for _ in0..10{let status_shared = Arc::clone(&status);let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250));// Modify the locked jobs directlyletmut jobs = status_shared.lock().unwrap();
jobs.jobs_done += 1;// Use 'jobs', not 'status_shared'});
handles.push(handle);}// Waiting for all jobs to complete.for handle in handles {
handle.join().unwrap();}// Print the value of `JobStatus.jobs_done`.println!("Jobs done: {}", status.lock().unwrap().jobs_done);}
Activity
simon-liebehenschel commentedon Sep 15, 2025
I think there are too many ways to pass the task. Here is another (most likely "wrong") solution:
threads2: Assert jobs_done == 10 to reject non-shared solutions (rust…