Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 03-May-2022 | - | ||||
.gitignore | H A D | 06-Mar-2018 | 14 | |||
.travis.yml | H A D | 06-Mar-2018 | 159 | |||
LICENSE | H A D | 06-Mar-2018 | 11.1 KiB | |||
README.md | H A D | 06-Mar-2018 | 1.8 KiB | |||
executor.go | H A D | 06-Mar-2018 | 613 | |||
go_above_19.go | H A D | 06-Mar-2018 | 209 | |||
go_below_19.go | H A D | 06-Mar-2018 | 642 | |||
log.go | H A D | 06-Mar-2018 | 301 | |||
map_test.go | H A D | 06-Mar-2018 | 269 | |||
test.sh | H A D | 06-Mar-2018 | 289 | |||
unbounded_executor.go | H A D | 06-Mar-2018 | 3.6 KiB | |||
unbounded_executor_test.go | H A D | 06-Mar-2018 | 1.1 KiB |
README.md
1# concurrent 2 3[![Sourcegraph](https://sourcegraph.com/github.com/modern-go/concurrent/-/badge.svg)](https://sourcegraph.com/github.com/modern-go/concurrent?badge) 4[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/modern-go/concurrent) 5[![Build Status](https://travis-ci.org/modern-go/concurrent.svg?branch=master)](https://travis-ci.org/modern-go/concurrent) 6[![codecov](https://codecov.io/gh/modern-go/concurrent/branch/master/graph/badge.svg)](https://codecov.io/gh/modern-go/concurrent) 7[![rcard](https://goreportcard.com/badge/github.com/modern-go/concurrent)](https://goreportcard.com/report/github.com/modern-go/concurrent) 8[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://raw.githubusercontent.com/modern-go/concurrent/master/LICENSE) 9 10* concurrent.Map: backport sync.Map for go below 1.9 11* concurrent.Executor: goroutine with explicit ownership and cancellable 12 13# concurrent.Map 14 15because sync.Map is only available in go 1.9, we can use concurrent.Map to make code portable 16 17```go 18m := concurrent.NewMap() 19m.Store("hello", "world") 20elem, found := m.Load("hello") 21// elem will be "world" 22// found will be true 23``` 24 25# concurrent.Executor 26 27```go 28executor := concurrent.NewUnboundedExecutor() 29executor.Go(func(ctx context.Context) { 30 everyMillisecond := time.NewTicker(time.Millisecond) 31 for { 32 select { 33 case <-ctx.Done(): 34 fmt.Println("goroutine exited") 35 return 36 case <-everyMillisecond.C: 37 // do something 38 } 39 } 40}) 41time.Sleep(time.Second) 42executor.StopAndWaitForever() 43fmt.Println("executor stopped") 44``` 45 46attach goroutine to executor instance, so that we can 47 48* cancel it by stop the executor with Stop/StopAndWait/StopAndWaitForever 49* handle panic by callback: the default behavior will no longer crash your application