-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: check if a thread is still alive after the block is dead #1875
base: main
Are you sure you want to change the base?
chore: check if a thread is still alive after the block is dead #1875
Conversation
Artifacts upload triggered. View details here |
b043b17
to
21f541e
Compare
Artifacts upload triggered. View details here |
21f541e
to
b043b17
Compare
Artifacts upload triggered. View details here |
b043b17
to
95cbeac
Compare
Artifacts upload triggered. View details here |
Artifacts upload triggered. View details here |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1875 +/- ##
==========================================
- Coverage 40.10% 9.68% -30.43%
==========================================
Files 26 133 +107
Lines 1895 16999 +15104
Branches 1895 16999 +15104
==========================================
+ Hits 760 1646 +886
- Misses 1100 15029 +13929
- Partials 35 324 +289 ☔ View full report in Codecov by Sentry. |
95cbeac
to
50376de
Compare
Artifacts upload triggered. View details here |
50376de
to
e69a679
Compare
Artifacts upload triggered. View details here |
e69a679
to
6439574
Compare
Artifacts upload triggered. View details here |
6439574
to
a0d8e60
Compare
Artifacts upload triggered. View details here |
a0d8e60
to
efda8d0
Compare
Artifacts upload triggered. View details here |
efda8d0
to
3507ca0
Compare
Artifacts upload triggered. View details here |
fn thread_logic(shared_struct: Arc<Mutex<Option<MyStruct>>>) { | ||
let mut shared_struct = shared_struct.lock().unwrap(); | ||
*shared_struct = Some(MyStruct { a: 1, b: 2 }); | ||
loop { | ||
let my_struct = shared_struct.as_ref().unwrap(); | ||
println!("MyStruct is accessible: a = {}, b = {}", my_struct.a, my_struct.b); | ||
thread::sleep(std::time::Duration::from_secs(1)); | ||
if my_struct.a >= 2 && my_struct.a <= 5 { | ||
println!("Mystuct.a: {} is between 2 and 5", my_struct.a); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thread_logic
function currently holds the mutex lock for the entire duration of the infinite loop, which creates a high risk of deadlock. The lock should be acquired and released in each loop iteration. Here's a safer approach:
let my_struct = {
let guard = shared_struct.lock().unwrap();
guard.as_ref().unwrap().clone()
};
This pattern ensures the mutex is released promptly after each read operation, allowing other threads to access the shared data structure.
Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.
3507ca0
to
238aed1
Compare
Artifacts upload triggered. View details here |
238aed1
to
843ef70
Compare
Artifacts upload triggered. View details here |
No description provided.