From bb1ac4fe4086674eaaeccb3b5fa3a30e05d05fac Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 8 Jul 2017 14:54:39 +0200 Subject: [PATCH] Concurrency tests --- .gitignore | 1 + pinger/main.go | 29 +++++++++++++++++++++++++++++ sandbox/main.go | 17 +++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 pinger/main.go create mode 100644 sandbox/main.go diff --git a/.gitignore b/.gitignore index d401de0..25977dc 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ my-fnv/my-fnv my-md5/my-md5 shadir/shadir shadirconcurrent/shadirconcurrent +pinger/pinger diff --git a/pinger/main.go b/pinger/main.go new file mode 100644 index 0000000..68c9417 --- /dev/null +++ b/pinger/main.go @@ -0,0 +1,29 @@ +package main + +import "fmt" +import "time" + +func pinger(c chan string) { + c <- "ping" +} +func ponger(c1 chan string, c2 chan string) { + <-c1 + c2 <- "pong" +} +func printer(c chan string) { + msg := <-c + fmt.Println(msg) + time.Sleep(time.Second) +} + +func main() { + pingChan, pongChan := make(chan string), make(chan string) + + go pinger(pingChan) + go ponger(pingChan, pongChan) + go printer(pongChan) + + var input string + fmt.Scanln(&input) + +} diff --git a/sandbox/main.go b/sandbox/main.go new file mode 100644 index 0000000..f197c3e --- /dev/null +++ b/sandbox/main.go @@ -0,0 +1,17 @@ +package main + +import "fmt" +import "sync" + +var wg sync.WaitGroup + +func main() { + for i := 0; i < 5; i++ { + go func() { + defer wg.Done() + wg.Add(1) + fmt.Println("Hello World") + }() + } + wg.Wait() +}