Skip to content

Proposal: add sync.Future #17483

Closed
Closed
@funny-falcon

Description

@funny-falcon

"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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions