Skip to content

Commit 82581bf

Browse files
author
hamed
committed
Goroutine introduction
1 parent da366b6 commit 82581bf

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

15-Concurrency/03-Intro/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module intro
2+
3+
go 1.18

15-Concurrency/03-Intro/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"runtime"
5+
"time"
6+
)
7+
8+
func main() {
9+
runtime.GOMAXPROCS(runtime.NumCPU())
10+
value := 0
11+
go task1()
12+
go task2()
13+
go task3()
14+
15+
go func() {
16+
// lock
17+
value++ //1
18+
//unlock
19+
}()
20+
go func() {
21+
// lock
22+
value+=2 // 3
23+
//unlock
24+
}()
25+
go func() {
26+
// lock
27+
value+=3 // 6
28+
//unlock
29+
}()
30+
31+
println(value)
32+
33+
time.Sleep(time.Second)
34+
}
35+
36+
func task1() {
37+
println("task1")
38+
}
39+
40+
func task2() {
41+
println("task2")
42+
}
43+
44+
func task3() {
45+
println("task3")
46+
}
47+
48+
// race condition
49+
//await

0 commit comments

Comments
 (0)