Skip to content

Commit fe1861f

Browse files
committed
Tidy threading
1 parent 5792f82 commit fe1861f

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/year2022/day11.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ struct Shared<'a> {
6868
}
6969

7070
struct Exclusive {
71-
pairs: Vec<Pair>,
7271
business: Business,
7372
}
7473

@@ -102,7 +101,7 @@ pub fn part2(input: &[Monkey]) -> u64 {
102101
}
103102

104103
/// Convenience wrapper to reuse common logic between part one and two.
105-
fn solve(monkeys: &[Monkey], play: impl Fn(&[Monkey], Vec<Pair>) -> Business) -> u64 {
104+
fn solve(monkeys: &[Monkey], play: impl Fn(&[Monkey], &[Pair]) -> Business) -> u64 {
106105
let mut pairs = Vec::new();
107106

108107
for (from, monkey) in monkeys.iter().enumerate() {
@@ -111,16 +110,16 @@ fn solve(monkeys: &[Monkey], play: impl Fn(&[Monkey], Vec<Pair>) -> Business) ->
111110
}
112111
}
113112

114-
let mut business = play(monkeys, pairs);
113+
let mut business = play(monkeys, &pairs);
115114
business.sort_unstable();
116115
business.iter().rev().take(2).product()
117116
}
118117

119118
/// Play 20 rounds dividing the worry level by 3 each inspection.
120-
fn sequential(monkeys: &[Monkey], pairs: Vec<Pair>) -> Business {
119+
fn sequential(monkeys: &[Monkey], pairs: &[Pair]) -> Business {
121120
let mut business = [0; 8];
122121

123-
for pair in pairs {
122+
for &pair in pairs {
124123
let extra = play(monkeys, 20, |x| x / 3, pair);
125124
business.iter_mut().enumerate().for_each(|(i, b)| *b += extra[i]);
126125
}
@@ -129,26 +128,20 @@ fn sequential(monkeys: &[Monkey], pairs: Vec<Pair>) -> Business {
129128
}
130129

131130
/// Play 10,000 rounds adjusting the worry level modulo the product of all the monkey's test values.
132-
fn parallel(monkeys: &[Monkey], pairs: Vec<Pair>) -> Business {
133-
let shared = Shared { monkeys, mutex: Mutex::new(Exclusive { pairs, business: [0; 8] }) };
131+
fn parallel(monkeys: &[Monkey], pairs: &[Pair]) -> Business {
132+
let shared = Shared { monkeys, mutex: Mutex::new(Exclusive { business: [0; 8] }) };
134133

135134
// Use as many cores as possible to parallelize the calculation.
136-
spawn(|| worker(&shared));
135+
spawn_parallel_iterator(pairs, |iter| worker(&shared, iter));
137136

138137
shared.mutex.into_inner().unwrap().business
139138
}
140139

141140
/// Multiple worker functions are executed in parallel, one per thread.
142-
fn worker(shared: &Shared<'_>) {
141+
fn worker(shared: &Shared<'_>, iter: ParIter<'_, Pair>) {
143142
let product: u64 = shared.monkeys.iter().map(|m| m.test).product();
144143

145-
loop {
146-
// Take an item from the queue until empty, using the mutex to allow access
147-
// to a single thread at a time.
148-
let Some(pair) = shared.mutex.lock().unwrap().pairs.pop() else {
149-
break;
150-
};
151-
144+
for &pair in iter {
152145
let extra = play(shared.monkeys, 10000, |x| x % product, pair);
153146

154147
let mut exclusive = shared.mutex.lock().unwrap();

0 commit comments

Comments
 (0)