Skip to content
/ event Public

Implemention of python's threading.Event using golang primitives

License

Notifications You must be signed in to change notification settings

trivigy/event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Implemention of python's threading.Event using golang primitives

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages