释放双眼,带上耳机,听听看~!
参考文档
无畏并发
大任务切成子任务派发到子线程处理
版本一
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 1#![feature(iterator_step_by)]
2#[macro_use]
3extern crate log;
4#[macro_use]
5extern crate scopeguard;
6extern crate num_cpus;
7extern crate log4rs;
8
9use std::time;
10use std::thread;
11use std::sync::{Arc, Mutex};
12
13fn sub_thread_func(task_list: Arc<Vec<i32>>, thread_index: usize, thread_count: usize, finished_num: Arc<Mutex<i64>>) {
14 info!("线程{} 启动", thread_index);
15 defer!( info!("线程{} 退出", thread_index) );
16
17 //使用类似Python中list步长的方法来进行遍历
18 for task_index in (thread_index..task_list.len()).step_by(thread_count) {
19 let task_id = task_list[task_index];
20 {
21 let mut fnum = finished_num.lock().unwrap();
22 *fnum += 1;
23 info!("线程{} 执行任务{} 已完成{}", thread_index, task_id, fnum);
24 }
25 thread::sleep(time::Duration::from_millis(500));
26 }
27}
28
29
30fn main() {
31 log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
32
33 //获取cpu的物理核心数量
34 //let thread_count = num_cpus::get_physical();
35
36 //获取cpu的逻辑核心数量
37 //let thread_count = num_cpus::get();
38
39 let thread_count = 4;
40
41
42 //待处理的任务队列
43 let task_list = Arc::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
44
45 //打印出耗费时间
46 let time_start = time::Instant::now();
47 info!("run start>>>>>");
48 defer!({
49 let elapsed = time_start.elapsed();
50 info!("run finish<<<<< ({}s {}ms)",elapsed.as_secs(), (elapsed.subsec_nanos() / 1_000_000) as u64);
51 });
52
53 //存储线程句柄
54 let mut thread_handlers = Vec::new();
55
56 //统计已完成的数量
57 let finished_num = Arc::new(Mutex::new(0i64));
58
59 //一次性均匀派发到各个子线程处理
60 for thread_index in 0..thread_count {
61 let task_list = task_list.clone();
62 let finished_num = finished_num.clone();
63
64 thread_handlers.push(thread::spawn(move || {
65 sub_thread_func(task_list, thread_index, thread_count, finished_num);
66 }))
67 }
68
69 //等待所有子进程退出
70 for handler in thread_handlers {
71 handler.join();
72 }
73}#![feature(iterator_step_by)]
74#[macro_use]
75extern crate log;
76#[macro_use]
77extern crate scopeguard;
78extern crate num_cpus;
79extern crate log4rs;
80
81use std::time;
82use std::thread;
83use std::sync::{Arc, Mutex};
84
85fn sub_thread_func(task_list: Arc<Vec<i32>>, thread_index: usize, thread_count: usize, finished_num: Arc<Mutex<i64>>) {
86 info!("线程{} 启动", thread_index);
87 defer!( info!("线程{} 退出", thread_index) );
88
89 //使用类似Python中list步长的方法来进行遍历
90 for task_index in (thread_index..task_list.len()).step_by(thread_count) {
91 let task_id = task_list[task_index];
92 {
93 let mut fnum = finished_num.lock().unwrap();
94 *fnum += 1;
95 info!("线程{} 执行任务{} 已完成{}", thread_index, task_id, fnum);
96 }
97 thread::sleep(time::Duration::from_millis(500));
98 }
99}
100
101
102fn main() {
103 log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
104
105 //获取cpu的物理核心数量
106 //let thread_count = num_cpus::get_physical();
107
108 //获取cpu的逻辑核心数量
109 //let thread_count = num_cpus::get();
110
111 let thread_count = 4;
112
113
114 //待处理的任务队列
115 let task_list = Arc::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
116
117 //打印出耗费时间
118 let time_start = time::Instant::now();
119 info!("run start>>>>>");
120 defer!({
121 let elapsed = time_start.elapsed();
122 info!("run finish<<<<< ({}s {}ms)",elapsed.as_secs(), (elapsed.subsec_nanos() / 1_000_000) as u64);
123 });
124
125 //存储线程句柄
126 let mut thread_handlers = Vec::new();
127
128 //统计已完成的数量
129 let finished_num = Arc::new(Mutex::new(0i64));
130
131 //一次性均匀派发到各个子线程处理
132 for thread_index in 0..thread_count {
133 let task_list = task_list.clone();
134 let finished_num = finished_num.clone();
135
136 thread_handlers.push(thread::spawn(move || {
137 sub_thread_func(task_list, thread_index, thread_count, finished_num);
138 }))
139 }
140
141 //等待所有子进程退出
142 for handler in thread_handlers {
143 handler.join();
144 }
145}
146
启动指定数量的线程,每个线程处理任务队列的一部分,主线程等待所有子线程退出