event

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2019 License: MIT Imports: 1 Imported by: 1

README

Event

CircleCI branch License GitHub tag (latest SemVer)

Introduction

Event is a simple locking primitives which allows for sending notifications across goroutines when an event has occurred.

Example

package main

import (
	"fmt"
	"sync"
	"time"
	
	"github.com/pkg/errors"
	"github.com/trivigy/event"
)

func main() {
	mutex := sync.Mutex{}
    	results := make([]error, 0)
    
    	start := sync.WaitGroup{}
    	finish := sync.WaitGroup{}
    	
    	N := 5
    	start.Add(N)
    	finish.Add(N)
    	for i := 0; i < N; i++ {
    		go func() {
    			start.Done()
    			
    			value := event.Wait(nil)
    			mutex.Lock()
    			results = append(results, value)
    			mutex.Unlock()
    
    			finish.Done()
    		}()
    	}
    
    	start.Wait()
    	time.Sleep(100 * time.Millisecond)
    	event.Set()
    	finish.Wait()
    	
    	fmt.Printf("%+v\n", results)
}

Documentation

Overview

Package event implements python's threading.Event api using golang primitives.

Index

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
}

Event is a communication primitive allowing for multiple goroutines to wait on an event to be set.

func New

func New() *Event

New creates a new event instance.

func (*Event) Clear

func (r *Event) Clear()

Clear resets the internal event state back to false.

func (*Event) IsSet

func (r *Event) IsSet() bool

IsSet indicates whether an event is set or not.

func (*Event) Set

func (r *Event) Set()

Set changes the internal state of an event to true.

func (*Event) Wait

func (r *Event) Wait(ctx context.Context) error

Wait blocks until the event is set to true. If the event is already set, returns immediately. Otherwise blocks until another goroutine sets the event.

Jump to

Keyboard shortcuts

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