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 (*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) 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 ¶
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