1
+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
1
2
use std:: sync:: mpsc:: channel;
3
+ use std:: sync:: { Arc , Mutex } ;
4
+ use std:: thread;
5
+ use std:: thread:: sleep;
2
6
use std:: time:: Duration ;
3
7
8
+ use fast_threadpool:: ThreadPoolConfig ;
4
9
use futures_lite:: * ;
5
10
use rayon;
6
- use threadpool:: ThreadPool ;
7
- use fast_threadpool:: ThreadPoolConfig ;
11
+ use rusty_pool;
8
12
9
13
fn fib ( n : usize ) -> usize {
10
14
if n == 0 || n == 1 {
@@ -26,7 +30,7 @@ pub fn rayon_threadpool() {
26
30
pub fn threadpool_example ( ) {
27
31
let n_workers = 4 ;
28
32
let n_jobs = 8 ;
29
- let pool = ThreadPool :: new ( n_workers) ;
33
+ let pool = threadpool :: ThreadPool :: new ( n_workers) ;
30
34
31
35
let ( tx, rx) = channel ( ) ;
32
36
for _ in 0 ..n_jobs {
@@ -41,19 +45,20 @@ pub fn threadpool_example() {
41
45
}
42
46
43
47
pub fn rusty_pool_example ( ) {
44
- let pool = ThreadPool :: new ( 4 ) ;
48
+ let pool = rusty_pool :: ThreadPool :: default ( ) ;
45
49
46
50
for _ in 1 ..10 {
47
51
pool. execute ( || {
48
52
println ! ( "Hello from a rusty_pool!" ) ;
49
53
} ) ;
50
54
}
51
55
52
- pool. join ( ) ;
56
+ pool. join ( ) ;
53
57
}
54
58
55
- pub fn fast_threadpool_example ( ) -> Result < ( ) , fast_threadpool:: ThreadPoolDisconnected > {
56
- let threadpool = fast_threadpool:: ThreadPool :: start ( ThreadPoolConfig :: default ( ) , ( ) ) . into_sync_handler ( ) ;
59
+ pub fn fast_threadpool_example ( ) -> Result < ( ) , fast_threadpool:: ThreadPoolDisconnected > {
60
+ let threadpool =
61
+ fast_threadpool:: ThreadPool :: start ( ThreadPoolConfig :: default ( ) , ( ) ) . into_sync_handler ( ) ;
57
62
58
63
assert_eq ! ( 4 , threadpool. execute( |_| { 2 + 2 } ) ?) ;
59
64
@@ -84,31 +89,145 @@ pub fn scheduled_thread_pool() {
84
89
let ( sender, receiver) = channel ( ) ;
85
90
86
91
let pool = scheduled_thread_pool:: ScheduledThreadPool :: new ( 4 ) ;
87
- let handle = pool. execute_after ( Duration :: from_millis ( 1000 ) , move ||{
92
+ let handle = pool. execute_after ( Duration :: from_millis ( 1000 ) , move || {
88
93
println ! ( "Hello from a scheduled thread!" ) ;
89
94
sender. send ( "done" ) . unwrap ( ) ;
90
95
} ) ;
91
96
92
-
93
97
let _ = handle;
94
98
receiver. recv ( ) . unwrap ( ) ;
99
+ }
100
+
101
+ // workerpool-rs
102
+ pub fn workerpool_rs_example ( ) {
103
+ use workerpool_rs:: pool:: WorkerPool ;
104
+
105
+ let n_workers = 4 ;
106
+ let n_jobs = 8 ;
107
+ let pool = WorkerPool :: new ( n_workers) ;
108
+
109
+ let ( tx, rx) = channel ( ) ;
110
+ let atx = Arc :: new ( Mutex :: new ( tx) ) ;
111
+ for _ in 0 ..n_jobs {
112
+ let atx = atx. clone ( ) ;
113
+ pool. execute ( move || {
114
+ let tx = atx. lock ( ) . unwrap ( ) ;
115
+ tx. send ( 1 )
116
+ . expect ( "channel will be there waiting for the pool" ) ;
117
+ } ) ;
118
+ }
119
+
120
+ // assert_eq!(rx.iter().take(n_jobs).fold(0, |a, b| a + b), 8);
121
+ println ! ( "{}" , rx. iter( ) . take( n_jobs) . fold( 0 , |a, b| a + b) )
122
+ }
123
+
124
+ fn test ( msg : usize ) {
125
+ println ! ( "key: {}\t value: {}" , msg, fib( msg) ) ;
126
+ }
127
+
128
+ // poolite
129
+ pub fn poolite_example ( ) {
130
+ let pool = poolite:: Pool :: new ( ) . unwrap ( ) ;
131
+ for i in 0 ..10 {
132
+ pool. push ( move || test ( i) ) ;
133
+ }
134
+
135
+ pool. join ( ) ; //wait for the pool
136
+ }
137
+
138
+ pub fn poolite_example2 ( ) {
139
+ let pool = poolite:: Pool :: new ( ) . unwrap ( ) ;
140
+ let mut array = ( 0 ..10usize ) . into_iter ( ) . map ( |i| ( i, 0 ) ) . collect :: < Vec < _ > > ( ) ;
141
+
142
+ // scoped method will waiting scoped's task running finish.
143
+ pool. scoped ( |scope| {
144
+ for i in array. iter_mut ( ) {
145
+ // have to move
146
+ scope. push ( move || i. 1 = i. 0 * i. 0 ) ;
147
+ }
148
+ } ) ;
95
149
150
+ for ( i, j) in array {
151
+ println ! ( "key: {}\t value: {}" , i, j) ;
152
+ }
96
153
}
97
154
98
- pub fn unblocking_smol ( ) -> io:: Result < ( ) > {
99
- smol:: block_on ( async {
100
- let mut stream = smol:: net:: TcpStream :: connect ( "example.com:80" ) . await ?;
101
- let req = b"GET / HTTP/1.1\r \n Host: example.com\r \n Connection: close\r \n \r \n " ;
102
- stream. write_all ( req) . await ?;
155
+ pub fn executor_service_example ( ) {
156
+ use executor_service:: Executors ;
157
+
158
+ let mut executor_service =
159
+ Executors :: new_fixed_thread_pool ( 10 ) . expect ( "Failed to create the thread pool" ) ;
160
+
161
+ let counter = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
162
+
163
+ for _ in 0 ..10 {
164
+ let counter = counter. clone ( ) ;
165
+ executor_service. execute ( move || {
166
+ thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
167
+ counter. fetch_add ( 1 , Ordering :: SeqCst ) ;
168
+ } ) ;
169
+ }
170
+
171
+ thread:: sleep ( Duration :: from_millis ( 1000 ) ) ;
172
+
173
+ assert_eq ! ( counter. load( Ordering :: SeqCst ) , 10 ) ;
103
174
104
- let mut stdout = smol:: Unblock :: new ( std:: io:: stdout ( ) ) ;
105
- io:: copy ( stream, & mut stdout) . await ?;
106
- Ok ( ( ) )
175
+ let mut executor_service =
176
+ Executors :: new_fixed_thread_pool ( 2 ) . expect ( "Failed to create the thread pool" ) ;
177
+
178
+ let some_param = "Mr White" ;
179
+ let res = executor_service
180
+ . submit_sync ( move || {
181
+ sleep ( Duration :: from_secs ( 5 ) ) ;
182
+ println ! ( "Hello {:}" , some_param) ;
183
+ println ! ( "Long computation finished" ) ;
184
+ 2
185
+ } )
186
+ . expect ( "Failed to submit function" ) ;
187
+
188
+ println ! ( "Result: {:#?}" , res) ;
189
+ assert_eq ! ( res, 2 ) ;
190
+ }
191
+
192
+ pub fn threadpool_executor_example ( ) {
193
+ let pool = threadpool_executor:: ThreadPool :: new ( 1 ) ;
194
+ let mut expectation = pool. execute ( || "hello, thread pool!" ) . unwrap ( ) ;
195
+ assert_eq ! ( expectation. get_result( ) . unwrap( ) , "hello, thread pool!" ) ;
196
+
197
+ let pool = threadpool_executor:: threadpool:: Builder :: new ( )
198
+ . core_pool_size ( 1 )
199
+ . maximum_pool_size ( 3 )
200
+ . keep_alive_time ( std:: time:: Duration :: from_secs ( 300 ) )
201
+ . exeed_limit_policy ( threadpool_executor:: threadpool:: ExceedLimitPolicy :: Wait )
202
+ . build ( ) ;
203
+
204
+ pool. execute ( || {
205
+ std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 3 ) ) ;
107
206
} )
207
+ . unwrap ( ) ;
208
+ let mut exp = pool. execute ( || { } ) . unwrap ( ) ;
209
+ exp. cancel ( ) ;
108
210
}
109
211
110
- // threads_pool
111
- // workerpool
112
- // poolite
212
+ pub fn executors_example ( ) {
213
+ use executors:: crossbeam_workstealing_pool;
214
+ use executors:: * ;
215
+ use std:: sync:: mpsc:: channel;
113
216
217
+ let n_workers = 4 ;
218
+ let n_jobs = 8 ;
219
+ let pool = crossbeam_workstealing_pool:: small_pool ( n_workers) ;
220
+
221
+ let ( tx, rx) = channel ( ) ;
222
+ for _ in 0 ..n_jobs {
223
+ let tx = tx. clone ( ) ;
224
+ pool. execute ( move || {
225
+ tx. send ( 1 )
226
+ . expect ( "channel will be there waiting for the pool" ) ;
227
+ } ) ;
228
+ }
229
+
230
+ assert_eq ! ( rx. iter( ) . take( n_jobs) . fold( 0 , |a, b| a + b) , 8 ) ;
231
+ }
114
232
233
+ // slave-pool
0 commit comments