Skip to content

Commit 1e902b4

Browse files
committed
time: clarify when draining a Timer's channel is needed
Updates golang#27169
1 parent a19c0ce commit 1e902b4

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/time/sleep.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ 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.
59-
// For example, assuming the program has not received from t.C already:
57+
// Programs should handle the case in which the timer fires just as
58+
// Stop is called by checking the return value.
59+
// For example, if t.C needs to be drained before calling Reset and
60+
// assuming the program has not received from t.C already:
6061
//
6162
// if !t.Stop() {
6263
// <-t.C
@@ -97,10 +98,9 @@ func NewTimer(d Duration) *Timer {
9798
// It returns true if the timer had been active, false if the timer had
9899
// expired or been stopped.
99100
//
100-
// Resetting a timer must take care not to race with the send into t.C
101-
// that happens when the current timer expires.
101+
// Reset should always be invoked on stopped or expired timers with a drained channel.
102102
// 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.
103+
// to have expired and the channel drained, so t.Reset can be used directly.
104104
// If a program has not yet received a value from t.C, however,
105105
// the timer must be stopped and—if Stop reports that the timer expired
106106
// before being stopped—the channel explicitly drained:

0 commit comments

Comments
 (0)