matklad / once_cell

Rust library for single assignment cells and lazy statics without macros
Apache License 2.0
1.87k stars 109 forks source link

Add OnceCell::wait #102

Closed matklad closed 2 years ago

matklad commented 4 years ago

This seems plausible:

impl OnceCell<T> {
  /// Blocks until the cell is populated
  fn wait(&self) -> &T {}
}

The implementaiton should basically stuff the current thread into the waiters list, without attempting to set the value.

That3Percent commented 4 years ago

For what it's worth, my opinion is that the addition of this API is a much better idea than making get block and #92 should not be merged.

matklad commented 2 years ago

Ironically, I need this now for http://github.com/near/nearcore/ :D

matklad commented 2 years ago

Hm, it's interesting that this is slightly not-trivial. Today, when a thread does get_or_try_init and fails (ie, it does not actually initialize the cell), it wakes all other waiters. This makes sense, as every waiter can set the cell.

With wait, we'll get waiters which ideally should not be woken up if we know that the cell is uninint.