File tree Expand file tree Collapse file tree 12 files changed +152
-4
lines changed Expand file tree Collapse file tree 12 files changed +152
-4
lines changed Original file line number Diff line number Diff line change @@ -7,3 +7,4 @@ edition = "2021"
7
7
8
8
[dependencies ]
9
9
beef = " 0.5.2"
10
+ once_cell = " 1.18.0"
Original file line number Diff line number Diff line change @@ -6,4 +6,5 @@ edition = "2021"
6
6
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
7
8
8
[dependencies ]
9
- rand = " 0.8.5"
9
+ once_cell = " 1.18.0"
10
+ rand = " 0.8.5"
Original file line number Diff line number Diff line change
1
+ use core:: time;
1
2
use std:: sync:: Arc ;
2
3
use std:: sync:: Barrier ;
3
4
use std:: thread;
@@ -21,6 +22,33 @@ pub fn barrier_example() {
21
22
} ) ) ;
22
23
}
23
24
25
+ for handle in handles {
26
+ handle. join ( ) . unwrap ( ) ;
27
+ }
28
+ }
29
+
30
+ pub fn barrier_recycle_example ( ) {
31
+ let barrier = Arc :: new ( Barrier :: new ( 10 ) ) ;
32
+ let mut handles = vec ! [ ] ;
33
+
34
+ for _ in 0 ..10 {
35
+ let barrier = barrier. clone ( ) ;
36
+ handles. push ( thread:: spawn ( move || {
37
+ println ! ( "before wait1" ) ;
38
+ let dur = rand:: thread_rng ( ) . gen_range ( 100 ..1000 ) ;
39
+ thread:: sleep ( std:: time:: Duration :: from_millis ( dur) ) ;
40
+
41
+ //step1
42
+ barrier. wait ( ) ;
43
+ println ! ( "after wait1" ) ;
44
+ thread:: sleep ( time:: Duration :: from_secs ( 1 ) ) ;
45
+
46
+ //step2
47
+ barrier. wait ( ) ;
48
+ println ! ( "after wait2" ) ;
49
+ } ) ) ;
50
+ }
51
+
24
52
for handle in handles {
25
53
handle. join ( ) . unwrap ( ) ;
26
54
}
Original file line number Diff line number Diff line change
1
+ use std:: sync:: Exclusive ;
2
+
3
+ pub fn exclusive_lock_example ( ) {
4
+ let mut exclusive = Exclusive :: new ( 92 ) ;
5
+ println ! ( "ready" ) ;
6
+ std:: thread:: spawn ( move || {
7
+ let counter = exclusive. get_mut ( ) ;
8
+ println ! ( "{}" , * counter) ;
9
+ * counter = 100 ;
10
+ } ) . join ( ) . unwrap ( ) ;
11
+
12
+ }
Original file line number Diff line number Diff line change
1
+ use std:: cell:: LazyCell ;
2
+ use std:: sync:: LazyLock ;
3
+
4
+ pub fn lazy_cell_example ( ) {
5
+ let lazy: LazyCell < i32 > = LazyCell :: new ( || {
6
+ println ! ( "initializing" ) ;
7
+ 92
8
+ } ) ;
9
+ println ! ( "ready" ) ;
10
+ println ! ( "{}" , * lazy) ;
11
+ println ! ( "{}" , * lazy) ;
12
+ }
13
+
14
+ use std:: collections:: HashMap ;
15
+ static HASHMAP : LazyLock < HashMap < i32 , String > > = LazyLock :: new ( || {
16
+ println ! ( "initializing" ) ;
17
+ let mut m = HashMap :: new ( ) ;
18
+ m. insert ( 13 , "Spica" . to_string ( ) ) ;
19
+ m. insert ( 74 , "Hoyten" . to_string ( ) ) ;
20
+ m
21
+ } ) ;
22
+
23
+ pub fn lazy_lock_example ( ) {
24
+ println ! ( "ready" ) ;
25
+ std:: thread:: spawn ( || {
26
+ println ! ( "{:?}" , HASHMAP . get( & 13 ) ) ;
27
+ } ) . join ( ) . unwrap ( ) ;
28
+ println ! ( "{:?}" , HASHMAP . get( & 74 ) ) ;
29
+ }
Original file line number Diff line number Diff line change 1
1
#![ feature( sync_unsafe_cell) ]
2
+ #![ feature( lazy_cell) ]
3
+ #![ feature( exclusive_wrapper) ]
2
4
3
5
pub mod arc;
4
6
pub mod mutex;
@@ -8,6 +10,8 @@ pub mod barrier;
8
10
pub mod cond;
9
11
pub mod mpsc;
10
12
pub mod atomic;
13
+ pub mod lazy;
14
+ pub mod exclusive;
11
15
12
16
pub use arc:: * ;
13
17
pub use mutex:: * ;
@@ -16,4 +20,6 @@ pub use once::*;
16
20
pub use barrier:: * ;
17
21
pub use cond:: * ;
18
22
pub use mpsc:: * ;
19
- pub use atomic:: * ;
23
+ pub use atomic:: * ;
24
+ pub use lazy:: * ;
25
+ pub use exclusive:: * ;
Original file line number Diff line number Diff line change @@ -8,14 +8,17 @@ fn main() {
8
8
mutex_example1 ( ) ;
9
9
mutex_example2_poison ( ) ;
10
10
mutex_example3_drop ( ) ;
11
-
11
+
12
12
rwlock_example ( ) ;
13
+ read_after_write ( ) ;
13
14
14
15
once_example ( ) ;
15
16
oncecell_example ( ) ;
16
17
oncelock_example ( ) ;
18
+ once_cell_example ( ) ;
17
19
18
20
barrier_example ( ) ;
21
+ barrier_recycle_example ( ) ;
19
22
20
23
condvar_example ( ) ;
21
24
condvar_example2 ( ) ;
@@ -25,4 +28,9 @@ fn main() {
25
28
26
29
atomic_example ( ) ;
27
30
atomic_example2 ( ) ;
31
+
32
+ lazy_cell_example ( ) ;
33
+ lazy_lock_example ( ) ;
34
+
35
+ exclusive_lock_example ( ) ;
28
36
}
Original file line number Diff line number Diff line change @@ -84,3 +84,4 @@ pub fn mutex_example3_drop() {
84
84
85
85
println ! ( "Result: {:?}" , res_mutex. lock( ) . unwrap( ) ) ;
86
86
}
87
+
Original file line number Diff line number Diff line change @@ -53,4 +53,17 @@ pub fn oncelock_example() {
53
53
assert_eq ! ( value. unwrap( ) . as_str( ) , "Hello, World!" ) ;
54
54
55
55
println ! ( "OnceLock: {}" , value. is_some( ) )
56
+ }
57
+
58
+ use std:: { sync:: Mutex , collections:: HashMap } ;
59
+ use once_cell:: sync:: Lazy ;
60
+ static GLOBAL_DATA : Lazy < Mutex < HashMap < i32 , String > > > = Lazy :: new ( || {
61
+ let mut m = HashMap :: new ( ) ;
62
+ m. insert ( 13 , "Spica" . to_string ( ) ) ;
63
+ m. insert ( 74 , "Hoyten" . to_string ( ) ) ;
64
+ Mutex :: new ( m)
65
+ } ) ;
66
+
67
+ pub fn once_cell_example ( ) {
68
+ println ! ( "{:?}" , GLOBAL_DATA . lock( ) . unwrap( ) ) ;
56
69
}
Original file line number Diff line number Diff line change @@ -35,4 +35,53 @@ pub fn rwlock_example() {
35
35
}
36
36
37
37
println ! ( "RwLock: {}" , * rwlock. read( ) . unwrap( ) ) ;
38
- }
38
+ }
39
+ pub fn read_after_write ( ) {
40
+ // 创建一个可共享的可变整数,使用RwLock包装
41
+ let counter = Arc :: new ( RwLock :: new ( 0 ) ) ;
42
+
43
+ // 创建一个线程持有读锁
44
+ let read_handle = {
45
+ let counter = counter. clone ( ) ;
46
+ thread:: spawn ( move || {
47
+ // 获取读锁
48
+ let num = counter. read ( ) . unwrap ( ) ;
49
+ println ! ( "Reader#1: {}" , * num) ;
50
+
51
+ // 休眠模拟读取操作
52
+ thread:: sleep ( std:: time:: Duration :: from_secs ( 10 ) ) ;
53
+ } )
54
+ } ;
55
+
56
+ // 创建一个线程请求写锁
57
+ let write_handle = {
58
+ let counter = counter. clone ( ) ;
59
+ thread:: spawn ( move || {
60
+ // 休眠一小段时间,确保读锁已经被获取
61
+ thread:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) ;
62
+
63
+ // 尝试获取写锁
64
+ let mut num = counter. write ( ) . unwrap ( ) ;
65
+ * num += 1 ;
66
+ println ! ( "Writer : Incremented counter to {}" , * num) ;
67
+ } )
68
+ } ;
69
+
70
+ // 创建一个线程请求读锁
71
+ let read_handle_2 = {
72
+ let counter = counter. clone ( ) ;
73
+ thread:: spawn ( move || {
74
+ // 休眠一小段时间,确保写锁已经被获取
75
+ thread:: sleep ( std:: time:: Duration :: from_secs ( 2 ) ) ;
76
+
77
+ // 尝试获取读锁
78
+ let num = counter. read ( ) . unwrap ( ) ;
79
+ println ! ( "Reader#2: {}" , * num) ;
80
+ } )
81
+ } ;
82
+
83
+ // 等待读取线程和写入线程完成
84
+ read_handle. join ( ) . unwrap ( ) ;
85
+ write_handle. join ( ) . unwrap ( ) ;
86
+ read_handle_2. join ( ) . unwrap ( ) ;
87
+ }
You can’t perform that action at this time.
0 commit comments