# Threads waiting for a worker thread to make progress

**URL:** <https://racket.discourse.group/t/threads-waiting-for-a-worker-thread-to-make-progress/507>\
**Category:** General\
**Created:** [January 1, 2022, 5:37pm UTC](https://racket.discourse.group/t/threads-waiting-for-a-worker-thread-to-make-progress/507 "2022-01-01T17:37:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)\
**Post date:** [January 1, 2022, 5:37pm UTC](https://racket.discourse.group/t/threads-waiting-for-a-worker-thread-to-make-progress/507/1 "2022-01-01T17:37:33Z")

</div>

If anyone would like to help review a little thread synchronization, I have an example.

The motivation:

- A worker thread updates (`set!`s) a couple increasing integer variables.

- Some other threads need to wait until those variables are `>=` certain values.

- Instead of the waiters inefficiently busy-looping, I wanted to arrange it so that the worker thread making progress could wake up each waiter when its desired condition is reached.

- Note: The values always increase, so if a condition becomes true, it remains true. Which I think simplifies this a little.

The least-worst thing I was able to come up with so far:

> <https://github.com/greghendershott/racket-mode/blob/6494baaca4fde4f4d496953ad4da792fcdb59b00/racket/hash-lang.rkt#L693-L780>

This performs well (enough for my benchmarks) and has no bugs (that I've discovered yet).

But I can't help wondering if I'm:

- Overlooking some other, better way to do this in Racket?

- Re-inventing some wheel? (This feels vaguely like so-called "condition variables" but I have zero hands-on mileage with that; from what I've read so far that feels more general than what I need here.)

---

<div class="post-metadata">

**Author:** ![SamPhillips](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samphillips/32/15_2.png) [@SamPhillips](https://racket.discourse.group/u/SamPhillips)\
**Post date:** [January 1, 2022, 8:36pm UTC](https://racket.discourse.group/t/threads-waiting-for-a-worker-thread-to-make-progress/507/2 "2022-01-01T20:36:15Z")

</div>

I'm a fan of channels and threads myself.

(Lightly tested) [counter.rkt · GitHub](https://gist.github.com/samdphillips/7c2ffd12aa8ce21635cac6b707a54c8e)

---

<div class="post-metadata">

**Author:** ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)\
**Post date:** [January 1, 2022, 10:02pm UTC](https://racket.discourse.group/t/threads-waiting-for-a-worker-thread-to-make-progress/507/3 "2022-01-01T22:02:37Z")

</div>

Thanks I'll take a look at that.

> I'm a fan of channels and threads myself.

Me, too! Channels are usually where I start, and nearly as often where I end up.

Although the example code shows it, I didn't really call out one aspect: There won't be a waiter interested in every -- or even most! -- progress updates.

For example some waiter might want to know when a counter reaches at least 5, but nothing before or after.

As a result, I assumed it would be wasteful to dump every single progress update into a channel?

Instead I figured the worker/updater should check a list of predicates, and IFF any became satisfied, alert/wake a waiter. Since there's no value to communicate except "hey your condition was met", I figured a semaphore would suffice. But a channel with a `#t` value could work for that instead.
