hrtime

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

hrtime

High resolution timer functions in Go

Rationale

The resolution of the time functions in the standard golang library can have poor resolution (at or greater than 1 ms). This library aims to provide higher resolution timer functions. Currently, it uses the hrtimer syscall interface, which means it will certainly only work on POSIX systems, and likely not all (for example, it does not work on Darwin/MacOS).

tick Timer

This module provides a tick timer based on hrtimer using a monotonic clock:

package main

import (
	"fmt"
	"time"

	"github.com/blorticus-go/hrtime"
)

func main() {
	ticker := hrtime.NewMonotonicTicker(100 * time.Microsecond)

	if err := ticker.Start(); err != nil {
		panic(err)
	}

	tickCounter := 0
	for {
		<-ticker.C
        go doSomethingUseful()
		tickCounter++
		if tickCounter == 100000 { // 10 seconds have elapsed
			break
		}
	}

	ticker.Stop()
}

Keep in mind that a call to the ticker channel is not guaranteed to happen within a single interval period. When the resolution is small (as in this example, with a resolution of 100 µs), the amount of processing time surrounding the channel functions can sometimes (and at small enough resolutions, often or always) take longer than resolution period. The channel returns the number of ticks that occurred since the last channel read.

It is also important to note that implementations of hrtimer don't have infinite granularity. On any platform, you will eventually hit the minimum tick limit.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MonotonicTicker

type MonotonicTicker struct {
	// After the ticker is started (using Start()), periodic
	// writes will occur on this channel.  The value is the
	// approximate number of ticks that have occurred since the
	// last channel read.  However, while the channel blocks
	// for the receiver, it does not block from the ticker, so
	// if a read is missed, one or more ticks may be lost.
	C chan uint64
	// contains filtered or unexported fields
}

A MonotonicTicker is a ticker using a monotonic clock.

func NewMonotonicTicker

func NewMonotonicTicker(interval time.Duration) *MonotonicTicker

NewMonotonicTicker creates a ticker that is intended to fire a tick near every interval.

func (*MonotonicTicker) Start

func (ticker *MonotonicTicker) Start() error

Start starts the ticker. Writes to ticker.C should now occur according to the interval configured in the constructor. Each time Start() is run, the ticker.C channel is replaced with a new one. Once a ticker is started, Start() cannot be run again until Stop() is run on the ticker.

func (*MonotonicTicker) Stop

func (ticker *MonotonicTicker) Stop() error

Stop stops a running ticker. The associated channel will be closed from the ticker side.

Jump to

Keyboard shortcuts

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