events

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: BSD-3-Clause Imports: 3 Imported by: 1

README


This package provides a simple and efficient implementation of an event signaling mechanism in Go. It allows you to manage an internal flag that can be set to true and reset to false, enabling synchronization between goroutines.

Features:

  • Set: Set the internal flag to true, waking up any goroutines waiting for it to be true.
  • Clear: Reset the internal flag to false, causing subsequent wait calls to block until the flag is set again.
  • IsSet: Check if the event is currently set.
  • Wait: Block until the internal flag is set or a specified timeout elapses.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// contains filtered or unexported fields
}

An event manages an internal flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true. The flag is initially false.

func New

func New() *Event

Creates new Event instance.

func (*Event) Clear

func (e *Event) Clear()

Reset the internal flag to false. Subsequently, threads calling wait() will block until set() is called to set the internal flag to true again.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/events"
)

func main() {
	e := events.New()
	e.Set()
	fmt.Println(e.IsSet())

	// Clear the event
	e.Clear()
	fmt.Println(e.IsSet())

}
Output:

true
false

func (*Event) IsSet

func (e *Event) IsSet() bool

Check event state whether it's Set or Clear.

func (*Event) Set

func (e *Event) Set()

Set the internal flag to true. All threads waiting for it to become true are awakened. Threads that call wait() once the flag is true will not block at all.

Example
package main

import (
	"fmt"

	"github.com/exonlabs/go-utils/pkg/events"
)

func main() {
	e := events.New()

	// Initially, the event is not set
	fmt.Println(e.IsSet())

	// Set the event
	e.Set()
	fmt.Println(e.IsSet())

}

func (*Event) Wait

func (e *Event) Wait(timeout float64) bool

Wait blocks until timeout and returns true if the internal flag is not set before the timeout. If the internal flag is set before the timeout ends, wait returns immediately with false.

Example
package main

import (
	"fmt"
	"time"

	"github.com/exonlabs/go-utils/pkg/events"
)

func main() {
	e := events.New()

	// Start a goroutine that sets the event after a delay
	go func() {
		time.Sleep(10 * time.Millisecond)
		e.Set()
	}()

	// Wait for the event to be set with a timeout
	if e.Wait(1.0) {
		fmt.Println("Timed out") // Should not reach this if the event is set in time
	} else {
		fmt.Println("Event set before timeout") // Expected output
	}

}
Output:

Event set before timeout

Jump to

Keyboard shortcuts

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