Skip to content

What is the right way to use Atomics.notify return value? #1492

Open
@empijei

Description

@empijei

Currently Atomics.wait waits on a value and returns without waiting if the value has changed since it was loaded.

This means that the right way to deterministically use wait is looping:

for (;;) {
  Atomics.load(arr, 0)
  // Check if condition to exit has occourred, if so exit loop
  Atomics.wait(arr, 0, val);
  // Potentially check the return value for "not-equal"
}

On the other end of the communication a script must invoke Atomics.notify, which returns the amount of workers that were notified by this call.

I couldn't come up with the correct way to use this value without races:

  • If wait is executed in a loop, a change in the array might cause the looping end to return before a wait, making notify always return 0 even if the other thread got the message.
  • If wait and notify are not executed in a loop, there is a clear race between them, and I wonder what is the intended use case for this (a notify could get lost if it wins the race)
  • If notify is executed in a loop, it can only keep notifying in a tight loop until it wakes a thread, which doesn't seem like a good thing to do.

What is the intended use-case for the value returned by Atomics.notify?

Thanks in advance for the explanations.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions