Skip to content

Commit

Permalink
Merge pull request #1150 from JulianKnodt/main
Browse files Browse the repository at this point in the history
Change FAQ example with race conditions
  • Loading branch information
cuviper authored Apr 4, 2024
2 parents cf167b6 + dc74819 commit 9f1e96a
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,14 @@ this shortest route, you can just stop and avoid wasted effort. In
sequential land, you might model this "best result" as a shared value
like `Rc<Cell<usize>>` (here the `usize` represents the length of best
path found so far); in parallel land, you'd use a `Arc<AtomicUsize>`.
Now we can make our search function look like:

```rust
fn search(path: &Path, cost_so_far: usize, best_cost: &Arc<AtomicUsize>) {
fn search(path: &Path, cost_so_far: usize, best_cost: &AtomicUsize) {
if cost_so_far >= best_cost.load(Ordering::SeqCst) {
return;
}
...
best_cost.store(...);
// Using `fetch_min` to avoid a race condition, in case it changed since `load`.
best_cost.fetch_min(..., Ordering::SeqCst);
}
```

Expand Down

0 comments on commit 9f1e96a

Please sign in to comment.