Created
September 30, 2018 03:56
-
-
Save hysios/bf9cd932763d5c201a62fac76fbeef03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"log" | |
"sync" | |
"time" | |
) | |
type ErrorBox struct { | |
sync.Mutex | |
members []error | |
} | |
func (eb *ErrorBox) Push(err error) { | |
eb.Lock() | |
defer eb.Unlock() | |
eb.members = append(eb.members, err) | |
} | |
func (eb *ErrorBox) Pop() error { | |
eb.Lock() | |
defer eb.Unlock() | |
if len(eb.members) > 0 { | |
err := eb.members[0] | |
eb.members = eb.members[1:] | |
return err | |
} | |
return nil | |
} | |
var box = &ErrorBox{} | |
func main() { | |
for i := 0; i < 100000; i++ { | |
go box.Push(errors.New("hello World")) | |
} | |
for i := 0; i < 50000; i++ { | |
go box.Pop() | |
} | |
time.Sleep(time.Second) | |
log.Printf("box length: %d", len(box.members)) | |
fmt.Println("Hello, playground") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment