Skip to content

Latest commit

 

History

History
54 lines (43 loc) · 1.34 KB

README.md

File metadata and controls

54 lines (43 loc) · 1.34 KB

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)
}