-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
|
||
// Show I gets get reference by the go routine | ||
var ch = make(chan int) | ||
|
||
go func(x int, y int) { | ||
ch <- x + y | ||
}(100, 200) | ||
|
||
k := <-ch | ||
|
||
fmt.Printf("Channel Sum: %d\n", k) | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
|
||
var ch = make(chan int) | ||
|
||
go func(cnt int) { | ||
defer close(ch) | ||
for i := 0; i < cnt; i++ { | ||
ch <- i | ||
} | ||
}(10) | ||
|
||
// process the received values | ||
for i := range ch { | ||
fmt.Println(i) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
var wg sync.WaitGroup | ||
|
||
func main() { | ||
|
||
var ch = make(chan int) | ||
|
||
wg.Add(1) | ||
go func(cnt int) { | ||
defer close(ch) | ||
for i := 0; i < cnt; i++ { | ||
ch <- i | ||
} | ||
wg.Done() | ||
}(10000) | ||
|
||
wg.Add(1) | ||
// Note the this causes deadlock. The question is why? | ||
go func() { | ||
// process the received values using select | ||
for i := range ch { | ||
fmt.Println(i) | ||
} | ||
wg.Done() | ||
}() | ||
wg.Wait() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
var wg sync.WaitGroup | ||
|
||
func main() { | ||
var cnt int = 10000 | ||
|
||
// Note we can | ||
var ch = make(chan int, cnt) | ||
|
||
wg.Add(1) | ||
go func(cnt int) { | ||
defer close(ch) | ||
// Send everything without blocking | ||
for i := 0; i < cnt; i++ { | ||
ch <- i | ||
} | ||
wg.Done() | ||
}(cnt) | ||
|
||
wg.Add(1) | ||
// Note the this causes deadlock. The question is why? | ||
go func() { | ||
// process the received values using select | ||
for i := range ch { | ||
fmt.Println(i) | ||
} | ||
wg.Done() | ||
}() | ||
wg.Wait() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
var wg sync.WaitGroup | ||
|
||
func main() { | ||
|
||
var ch = make(chan int, 0) | ||
|
||
wg.Add(1) | ||
go func(cnt int) { | ||
defer close(ch) | ||
for i := 0; i < cnt; i++ { | ||
ch <- i | ||
} | ||
ch <- -1 | ||
wg.Done() | ||
}(10) | ||
|
||
wg.Add(1) | ||
// Note the this causes deadlock. The question is why? | ||
go func() { | ||
defer wg.Done() | ||
// process the received values using select | ||
for { | ||
select { | ||
case i := <-ch: | ||
if i < 0 { | ||
fmt.Println("Exiting") | ||
return | ||
} | ||
fmt.Println(i) | ||
/* | ||
case <-time.After(1 * time.Second): | ||
fmt.Println("timeout") | ||
return | ||
*/ | ||
} | ||
|
||
} | ||
}() | ||
wg.Wait() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
// This example comes from https://go.dev/doc/articles/race_detector | ||
// go run -race main.go | ||
func main() { | ||
c := make(chan bool) | ||
m := make(map[string]string) | ||
go func() { | ||
m["1"] = "a" // First conflicting access. | ||
c <- true | ||
}() | ||
m["2"] = "b" // Second conflicting access. | ||
<-c | ||
for k, v := range m { | ||
fmt.Println(k, v) | ||
} | ||
} |