Skip to content

Commit ba31496

Browse files
darkfelinerobpike
authored andcommitted
time: clarify when draining a Timer's channel is needed
Updates #27169 Change-Id: I22a6194c06529ba70b1ec648e3188c191224e321 GitHub-Last-Rev: 457b2a6 GitHub-Pull-Request: #32996 Reviewed-on: https://go-review.googlesource.com/c/go/+/185245 Reviewed-by: Rob Pike <r@golang.org>
1 parent 52fdd62 commit ba31496

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/time/sleep.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ type Timer struct {
5454
// Stop does not close the channel, to prevent a read from the channel succeeding
5555
// incorrectly.
5656
//
57-
// To prevent a timer created with NewTimer from firing after a call to Stop,
58-
// check the return value and drain the channel.
57+
// To ensure the channel is empty after a call to Stop, check the
58+
// return value and drain the channel.
5959
// For example, assuming the program has not received from t.C already:
6060
//
6161
// if !t.Stop() {
@@ -97,10 +97,9 @@ func NewTimer(d Duration) *Timer {
9797
// It returns true if the timer had been active, false if the timer had
9898
// expired or been stopped.
9999
//
100-
// Resetting a timer must take care not to race with the send into t.C
101-
// that happens when the current timer expires.
100+
// Reset should be invoked only on stopped or expired timers with drained channels.
102101
// If a program has already received a value from t.C, the timer is known
103-
// to have expired, and t.Reset can be used directly.
102+
// to have expired and the channel drained, so t.Reset can be used directly.
104103
// If a program has not yet received a value from t.C, however,
105104
// the timer must be stopped and—if Stop reports that the timer expired
106105
// before being stopped—the channel explicitly drained:

0 commit comments

Comments
 (0)