@@ -16,6 +16,9 @@ use thread_amount::thread_amount;
16
16
use thread_control:: * ;
17
17
use thread_priority:: * ;
18
18
19
+ #[ cfg( not( target_os = "macos" ) ) ]
20
+ use affinity:: * ;
21
+
19
22
pub fn start_one_thread ( ) {
20
23
let count = thread:: available_parallelism ( ) . unwrap ( ) . get ( ) ;
21
24
@@ -28,6 +31,18 @@ pub fn start_one_thread() {
28
31
handle. join ( ) . unwrap ( ) ;
29
32
}
30
33
34
+ pub fn start_one_thread_result ( ) {
35
+ let handle = thread:: spawn ( || {
36
+ println ! ( "Hello from a thread!" ) ;
37
+ 200
38
+ } ) ;
39
+
40
+ match handle. join ( ) {
41
+ Ok ( v) => println ! ( "thread result: {}" , v) ,
42
+ Err ( e) => println ! ( "error: {:?}" , e) ,
43
+ }
44
+ }
45
+
31
46
pub fn start_two_threads ( ) {
32
47
let handle1 = thread:: spawn ( || {
33
48
println ! ( "Hello from a thread1!" ) ;
@@ -41,6 +56,50 @@ pub fn start_two_threads() {
41
56
handle2. join ( ) . unwrap ( ) ;
42
57
}
43
58
59
+ pub fn start_n_threads ( ) {
60
+ const N : isize = 10 ;
61
+
62
+ let handles: Vec < _ > = ( 0 ..N )
63
+ . map ( |i| {
64
+ thread:: spawn ( move || {
65
+ println ! ( "Hello from a thread{}!" , i) ;
66
+ } )
67
+ } )
68
+ . collect ( ) ;
69
+
70
+ // handles.into_iter().for_each(|h| h.join().unwrap());
71
+
72
+ for handle in handles {
73
+ handle. join ( ) . unwrap ( ) ;
74
+ }
75
+ }
76
+
77
+ pub fn current_thread ( ) {
78
+ let current_thread = thread:: current ( ) ;
79
+ println ! (
80
+ "current thread: {:?},{:?}" ,
81
+ current_thread. id( ) ,
82
+ current_thread. name( )
83
+ ) ;
84
+
85
+ let builder = thread:: Builder :: new ( )
86
+ . name ( "foo" . into ( ) ) // set thread name
87
+ . stack_size ( 32 * 1024 ) ; // set stack size
88
+
89
+ let handler = builder
90
+ . spawn ( || {
91
+ let current_thread = thread:: current ( ) ;
92
+ println ! (
93
+ "child thread: {:?},{:?}" ,
94
+ current_thread. id( ) ,
95
+ current_thread. name( )
96
+ ) ;
97
+ } )
98
+ . unwrap ( ) ;
99
+
100
+ handler. join ( ) . unwrap ( ) ;
101
+ }
102
+
44
103
pub fn start_thread_with_sleep ( ) {
45
104
let handle1 = thread:: spawn ( || {
46
105
thread:: sleep ( Duration :: from_millis ( 2000 ) ) ;
@@ -56,6 +115,21 @@ pub fn start_thread_with_sleep() {
56
115
handle2. join ( ) . unwrap ( ) ;
57
116
}
58
117
118
+ pub fn start_thread_with_yield_now ( ) {
119
+ let handle1 = thread:: spawn ( || {
120
+ thread:: yield_now ( ) ;
121
+ println ! ( "yield_now!" ) ;
122
+ } ) ;
123
+
124
+ let handle2 = thread:: spawn ( || {
125
+ thread:: yield_now ( ) ;
126
+ println ! ( "yield_now in another thread!" ) ;
127
+ } ) ;
128
+
129
+ handle1. join ( ) . unwrap ( ) ;
130
+ handle2. join ( ) . unwrap ( ) ;
131
+ }
132
+
59
133
pub fn start_thread_with_priority ( ) {
60
134
let handle1 = thread:: spawn ( || {
61
135
assert ! ( set_current_thread_priority( ThreadPriority :: Min ) . is_ok( ) ) ;
@@ -97,13 +171,45 @@ pub fn start_one_thread_with_move() {
97
171
let x = 100 ;
98
172
99
173
let handle = thread:: spawn ( move || {
100
- println ! ( "Hello from a thread, x={}!" , x) ;
174
+ println ! ( "Hello from a thread with move , x={}!" , x) ;
101
175
} ) ;
102
176
103
177
handle. join ( ) . unwrap ( ) ;
178
+
179
+ let handle = thread:: spawn ( move || {
180
+ println ! ( "Hello from a thread with move again, x={}!" , x) ;
181
+ } ) ;
182
+ handle. join ( ) . unwrap ( ) ;
183
+
184
+ let handle = thread:: spawn ( || {
185
+ println ! ( "Hello from a thread without move" ) ;
186
+ } ) ;
187
+ handle. join ( ) . unwrap ( ) ;
188
+
104
189
}
105
190
106
- pub fn start_one_thread_with_threadlocal ( ) {
191
+ // pub fn start_one_thread_with_move2() {
192
+ // let x = vec![1, 2, 3];
193
+
194
+ // let handle = thread::spawn(move || {
195
+ // println!("Hello from a thread with move, x={:?}!", x);
196
+ // });
197
+
198
+ // handle.join().unwrap();
199
+
200
+ // let handle = thread::spawn(move|| {
201
+ // println!("Hello from a thread with move again, x={:?}!", x);
202
+ // });
203
+ // handle.join().unwrap();
204
+
205
+ // let handle = thread::spawn(|| {
206
+ // println!("Hello from a thread without move");
207
+ // });
208
+ // handle.join().unwrap();
209
+
210
+ // }
211
+
212
+ pub fn start_threads_with_threadlocal ( ) {
107
213
thread_local ! ( static COUNTER : RefCell <u32 > = RefCell :: new( 1 ) ) ;
108
214
109
215
COUNTER . with ( |c| {
@@ -151,6 +257,18 @@ pub fn thread_park() {
151
257
handle. join ( ) . unwrap ( ) ;
152
258
}
153
259
260
+ pub fn thread_park2 ( ) {
261
+ let handle = thread:: spawn ( || {
262
+ thread:: sleep ( Duration :: from_millis ( 1000 ) ) ;
263
+ thread:: park ( ) ;
264
+ println ! ( "Hello from a park thread in case of unpark first!" ) ;
265
+ } ) ;
266
+
267
+ handle. thread ( ) . unpark ( ) ;
268
+
269
+ handle. join ( ) . unwrap ( ) ;
270
+ }
271
+
154
272
pub fn thread_park_timeout ( ) {
155
273
let handle = thread:: spawn ( || {
156
274
thread:: park_timeout ( Duration :: from_millis ( 1000 ) ) ;
@@ -159,6 +277,25 @@ pub fn thread_park_timeout() {
159
277
handle. join ( ) . unwrap ( ) ;
160
278
}
161
279
280
+ // pub fn wrong_start_threads_without_scoped() {
281
+ // let mut a = vec![1, 2, 3];
282
+ // let mut x = 0;
283
+
284
+ // thread::spawn(move || {
285
+ // println!("hello from the first scoped thread");
286
+ // dbg!(&a);
287
+ // });
288
+ // thread::spawn(move || {
289
+ // println!("hello from the second scoped thread");
290
+ // x += a[0] + a[2];
291
+ // });
292
+ // println!("hello from the main thread");
293
+
294
+ // // After the scope, we can modify and access our variables again:
295
+ // a.push(4);
296
+ // assert_eq!(x, a.len());
297
+ // }
298
+
162
299
pub fn start_scoped_threads ( ) {
163
300
let mut a = vec ! [ 1 , 2 , 3 ] ;
164
301
let mut x = 0 ;
@@ -223,6 +360,21 @@ pub fn rayon_scope() {
223
360
assert_eq ! ( x, a. len( ) ) ;
224
361
}
225
362
363
+
364
+ // pub fn wrong_send() {
365
+ // let counter = Rc::new(42);
366
+
367
+ // let (sender, receiver) = channel();
368
+
369
+ // let _t = thread::spawn(move || {
370
+ // sender.send(counter).unwrap();
371
+ // });
372
+
373
+ // let value = receiver.recv().unwrap();
374
+
375
+ // println!("received from the main thread: {}", value);
376
+ // }
377
+
226
378
pub fn send_wrapper ( ) {
227
379
let wrapped_value = SendWrapper :: new ( Rc :: new ( 42 ) ) ;
228
380
@@ -326,3 +478,20 @@ pub fn park_thread() {
326
478
327
479
println ! ( "park_unpark" )
328
480
}
481
+
482
+ pub fn info ( ) {
483
+ let count = thread:: available_parallelism ( ) . unwrap ( ) . get ( ) ;
484
+ println ! ( "available_parallelism: {}" , count) ;
485
+
486
+ if let Some ( count) = num_threads:: num_threads ( ) {
487
+ println ! ( "num_threads: {}" , count) ;
488
+ } else {
489
+ println ! ( "num_threads: not supported" ) ;
490
+ }
491
+
492
+ let count = thread_amount:: thread_amount ( ) ;
493
+ println ! ( "thread_amount: {}" , count. unwrap( ) ) ;
494
+
495
+ let count = num_cpus:: get ( ) ;
496
+ println ! ( "num_cpus: {}" , count) ;
497
+ }
0 commit comments