Channel:demand(timeout)

Issue #1344 resolved
Pablo Mayobre
created an issue

As proposed in the Discord #support chat.

Channel:demand() can be used to decrease the CPU consumption of a thread, yet there is no way to abort if a thread is not responding.

So in order to abort this operation, I propose that a timeout is added to Channel:demand() as an argument so: Channel:demand(timeout) where timeout is the number of seconds to wait.

After the timeout is met the function should either return nil or an error (which you can then safely isolate with pcall)

A similar thing could be done to Channel:supply() but I don't know how that would work since the argument may be interpreted as a value to supply.

Comments (2)

  1. Bart van Strien
    • changed status to open

    Though we have the primitives, it's possible for the thread to be get a signal, but for another thread to get the value. That leaves us three options:

    • Return nil early
    • Return nil "late", that is, keep waiting x seconds until you actually get a value
    • Keep track of time, and modify the wait time

    I think option 2 is the least desirable. Option 3 matches the user's expectation closest, but means the most work/overhead.

    Another alternative is an internal restructure. The reason there is a broadcast is because of supply. Theoretically supply and demand could work on separate signals, meaning only supply gets a broadcast, and whatever thread gets woken in demand is almost guaranteed to get the value. That does introduce a race condition though, and also prevents a similar technique for supply.

  2. Log in to comment