MCPcopy Create free account
hub / github.com/CodeSentryAI/lockbud / std_correct

Function std_correct

toys/condvar-struct/src/main.rs:4–45  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2use std::thread;
3
4fn std_correct() {
5 use std::sync::{Condvar, Mutex};
6
7 struct CondPair {
8 lock: Mutex<bool>,
9 cvar: Condvar,
10 }
11
12 impl CondPair {
13 fn new() -> Self {
14 Self {
15 lock: Mutex::new(false),
16 cvar: Condvar::new(),
17 }
18 }
19 fn wait(&self) {
20 let mut started = self.lock.lock().unwrap();
21 println!("start waiting!");
22 while !*started {
23 started = self.cvar.wait(started).unwrap();
24 }
25 println!("end waiting");
26 }
27 fn notify(&self) {
28 let mut started = self.lock.lock().unwrap();
29 println!("start notifying!");
30 *started = true;
31 self.cvar.notify_one();
32 println!("end notifying!");
33 }
34 }
35
36 let condvar1 = Arc::new(CondPair::new());
37 let condvar2 = condvar1.clone();
38
39 let th1 = thread::spawn(move || {
40 condvar1.wait();
41 });
42
43 condvar2.notify();
44 th1.join().unwrap();
45}
46
47fn std_deadlock_wait() {
48 use std::sync::{Condvar, Mutex};

Callers 1

mainFunction · 0.70

Calls 2

waitMethod · 0.80
notifyMethod · 0.80

Tested by

no test coverage detected