Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: sync: add a Reset method for sync.Once #25955

Closed
SilverRainZ opened this issue Jun 19, 2018 · 1 comment
Closed

proposal: sync: add a Reset method for sync.Once #25955

SilverRainZ opened this issue Jun 19, 2018 · 1 comment

Comments

@SilverRainZ
Copy link
Contributor

SilverRainZ commented Jun 19, 2018

What version of Go are you using (go version)?

go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

N/A

What did you do?

Assuming that there are some structure which has Open and Close method. Some initial stuff should be done for only once - so they share a sync.Once to do this.

type St struct {
    openOnce *sync.Once
    closeOnce *sync.Once
}

func(st *St) Open(){
    st.openOnce.Do(func() { ... }) // init
    ...
}

func(st *St) Close(){
    st.closeOnce.Do(func() { ... }) // deinit
    ...
}

But in this case, St can not be opened again after closed because the "sync.Once.Do()" is actually run once.

What did you expect to see?

I hope this struct can be opened for multiple times, I think adding a Reset method is an idea - then we can reset the openOnce in the Close method.

What did you see instead?

There is not a method to reset the state of sync.Once.

@gopherbot gopherbot added this to the Proposal milestone Jun 19, 2018
@ianlancetaylor
Copy link
Contributor

Use ponce *sync.Once, and reset it by writing ponce = new(sync.Once).

Changing a pointer makes sense, but resetting a sync.Once does not. The whole point of a sync.Once is that it only happens once.

@golang golang locked and limited conversation to collaborators Jun 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants