Add OnceLock::wait
#405
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
Problem statement
OnceCell
is useful for synchronizing write-once data, but it does not have any blocking methods that allow other threads to wait for this data.Motivating examples or use cases
Sometimes it can be useful to allow the end of computation to return with a value across thread boundaries, when joining may not be acceptable. For example, rayon::spawn does not have a return value that allows for easy returning. Other applications are nested spawning, or cases where a worker thread should return a value (and unblock the parent thread) then continue going without joining.
The example from once_cell shows the basic structure:
Solution sketch
Add the following:
Alternatives
Approximating this behavior with other synchronization primitives, e.g.
Barrier
to wait for the thread andMutex
to store the return value, or just manually parking (which this method is a thin wrapper around).Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: