Closed
Description
"future" is well known useful concurrency primitive.
So it strangely missed from language and standard library.
Since #17466 (add to language) doubtfully will be accepted, may be it is possible to add it to stdlib.
Proposal
add sync.Future with following properties:
- it should provide method for getting wait channel to be used in
select
statement, - it should guarantee value is set only once even when concurrent goroutines tries to set it simultaneously,
- it may have convinient method for waiting for "completion" and getting value at once
As additional properties: - it may have separate field for storing error,
- then method for getting value will return both value and error,
As example implementation (but not final) I suggest following:
https://github.com/funny-falcon/go-datastructures/blob/future_selectable_lazy_chan/futures/selectable.go
Pros:
- fast check in
GetResult
on already filled future, - wait channel is lazy created,
- on
Fill
, pointer to channel is changed to single global closed chan, so per Future channel could be early collected by GC (or even not allocated at all)