mirror of
https://github.com/golang/go
synced 2024-11-23 16:40:03 -07:00
time: clarify when draining a Timer's channel is needed
Updates https://github.com/golang/go/issues/27169
This commit is contained in:
parent
a19c0ceda5
commit
457b2a61a8
@ -54,8 +54,8 @@ type Timer struct {
|
||||
// Stop does not close the channel, to prevent a read from the channel succeeding
|
||||
// incorrectly.
|
||||
//
|
||||
// To prevent a timer created with NewTimer from firing after a call to Stop,
|
||||
// check the return value and drain the channel.
|
||||
// To ensure the channel is empty after a call to Stop, check the
|
||||
// return value and drain the channel.
|
||||
// For example, assuming the program has not received from t.C already:
|
||||
//
|
||||
// if !t.Stop() {
|
||||
@ -97,10 +97,9 @@ func NewTimer(d Duration) *Timer {
|
||||
// It returns true if the timer had been active, false if the timer had
|
||||
// expired or been stopped.
|
||||
//
|
||||
// Resetting a timer must take care not to race with the send into t.C
|
||||
// that happens when the current timer expires.
|
||||
// Reset should be invoked only on stopped or expired timers with drained channels.
|
||||
// If a program has already received a value from t.C, the timer is known
|
||||
// to have expired, and t.Reset can be used directly.
|
||||
// to have expired and the channel drained, so t.Reset can be used directly.
|
||||
// If a program has not yet received a value from t.C, however,
|
||||
// the timer must be stopped and—if Stop reports that the timer expired
|
||||
// before being stopped—the channel explicitly drained:
|
||||
|
Loading…
Reference in New Issue
Block a user