safetime

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2018 License: MIT Imports: 2 Imported by: 1

README

safetime

This lib is try to solve go issue time timer.Reset is not thread safe

Here is a sample code

package main

import (
	"time"
)

func main() {
	tm := time.NewTimer(1 * time.Millisecond)
	for i := 0; i < 2; i++ {
		go func() {
			for {
				tm.Reset(1 * time.Millisecond)
			}
		}()
	}
	for {
		<-tm.C
	}
}

This code will be panic if you try to run.

With this lib safetime it will not panic any more.

package main

import (
	time "github.com/codeskyblue/safetime"
)

func main() {
	tm := time.NewTimer(1 * time.Millisecond)
	for i := 0; i < 2; i++ {
		go func() {
			for {
				tm.Reset(1 * time.Millisecond)
			}
		}()
	}
	for {
		<-tm.C
	}
}

panic in go 1.11

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Timer added in v0.2.0

type Timer struct {
	*time.Timer
	// contains filtered or unexported fields
}

SafeTime add thread-safe for time.Timer

func NewTimer added in v0.2.0

func NewTimer(d time.Duration) *Timer

func (*Timer) Reset added in v0.2.0

func (t *Timer) Reset(d time.Duration) bool

Reset is thread-safe now

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL