File: mutex_use.rs

package info (click to toggle)
rust-enclose 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 132 kB
  • sloc: makefile: 4
file content (31 lines) | stat: -rw-r--r-- 593 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

use std::sync::Arc;
use std::sync::Mutex;
use std::thread;

use enclose::enclose;

fn main() {
	let mutex_left_data = Arc::new(Mutex::new( 0 ));
	let right_data = Arc::new(1);
	
	let thread = thread::spawn( enclose!((mutex_left_data, right_data) move || {
		let mut lock = match mutex_left_data.lock() {
			Ok(a) => a,
			Err(e) => e.into_inner(),
		};
		*lock += *right_data;
	}));

	thread.join().unwrap();
	{
		let left_data = match mutex_left_data.lock() {
			Ok(a) => a,
			Err(e) => e.into_inner(),
		};
		
		assert_eq!(*left_data, *right_data);
		// if *left_data == *right_data
	}
}