File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // This sample program demonstrates how to create goroutines and
2+ // how the scheduler behaves.
3+ package main
4+
5+ import (
6+ "fmt"
7+ "sync"
8+ )
9+
10+ // main is the entry point for all Go programs.
11+ func main () {
12+ // wg is used to wait for the program to finish.
13+ // Add a count of two, one for each goroutine.
14+ var wg sync.WaitGroup
15+ wg .Add (2 )
16+
17+ fmt .Println ("Start Goroutines" )
18+
19+ // Declare an anonymous function and create a goroutine.
20+ go func () {
21+ // Schedule the call to Done to tell main we are done.
22+ defer wg .Done ()
23+
24+ // Display the alphabet three times
25+ for count := 0 ; count < 3 ; count ++ {
26+ for char := 'a' ; char < 'a' + 26 ; char ++ {
27+ fmt .Printf ("%c " , char )
28+ }
29+ }
30+ }()
31+
32+ // Declare an anonymous function and create a goroutine.
33+ go func () {
34+ // Schedule the call to Done to tell main we are done.
35+ defer wg .Done ()
36+
37+ // Display the alphabet three times
38+ for count := 0 ; count < 3 ; count ++ {
39+ for char := 'A' ; char < 'A' + 26 ; char ++ {
40+ fmt .Printf ("%c " , char )
41+ }
42+ }
43+ }()
44+
45+ // Wait for the goroutines to finish.
46+ fmt .Println ("Waiting To Finish" )
47+ wg .Wait ()
48+
49+ fmt .Println ("\\ nTerminating Program" )
50+ }
Original file line number Diff line number Diff line change 1+ // This sample program demonstrates how the goroutine scheduler
2+ // will time slice goroutines on a single thread.
3+ package main
4+
5+ import (
6+ "fmt"
7+ "sync"
8+ )
9+
10+ // wg is used to wait for the program to finish.
11+ var wg sync.WaitGroup
12+
13+ // main is the entry point for all Go programs.
14+ func main () {
15+ // Add a count of two, one for each goroutine.
16+ wg .Add (2 )
17+
18+ // Create two goroutines.
19+ fmt .Println ("Create Goroutines" )
20+ go printPrime ("A" )
21+ go printPrime ("B" )
22+
23+ // Wait for the goroutines to finish.
24+ fmt .Println ("Waiting To Finish" )
25+ wg .Wait ()
26+
27+ fmt .Println ("Terminating Program" )
28+ }
29+
30+ // printPrime displays prime numbers for the first 5000 numbers.
31+ func printPrime (prefix string ) {
32+ // Schedule the call to Done to tell main we are done.
33+ defer wg .Done ()
34+
35+ next:
36+ for outer := 1 ; outer < 5000 ; outer ++ {
37+ for inner := 2 ; inner < outer ; inner ++ {
38+ if outer % inner == 0 {
39+ continue next
40+ }
41+ }
42+ fmt .Printf ("%s:%d\\ n" , prefix , outer )
43+ }
44+ fmt .Println ("Completed" , prefix )
45+ }
Original file line number Diff line number Diff line change 1+ // This sample program demonstrates how to create goroutines and
2+ // how the goroutine scheduler behaves with two contexts.
3+ package main
4+
5+ import (
6+ "fmt"
7+ "runtime"
8+ "sync"
9+ )
10+
11+ // main is the entry point for all Go programs.
12+ func main () {
13+ // wg is used to wait for the program to finish.
14+ // Add a count of two, one for each goroutine.
15+ var wg sync.WaitGroup
16+ wg .Add (2 )
17+
18+ // Allocate two contexts for the scheduler to use.
19+ runtime .GOMAXPROCS (2 )
20+
21+ fmt .Println ("Start Goroutines" )
22+
23+ // Declare an anonymous function and create a goroutine.
24+ go func () {
25+ // Schedule the call to Done to tell main we are done.
26+ defer wg .Done ()
27+
28+ // Display the alphabet three times.
29+ for count := 0 ; count < 3 ; count ++ {
30+ for char := 'a' ; char < 'a' + 26 ; char ++ {
31+ fmt .Printf ("%c " , char )
32+ }
33+ }
34+ }()
35+
36+ // Declare an anonymous function and create a goroutine.
37+ go func () {
38+ // Schedule the call to Done to tell main we are done.
39+ defer wg .Done ()
40+
41+ // Display the alphabet three times.
42+ for count := 0 ; count < 3 ; count ++ {
43+ for char := 'A' ; char < 'A' + 26 ; char ++ {
44+ fmt .Printf ("%c " , char )
45+ }
46+ }
47+ }()
48+
49+ // Wait for the goroutines to finish.
50+ fmt .Println ("Waiting To Finish" )
51+ wg .Wait ()
52+
53+ fmt .Println ("\\ nTerminating Program" )
54+ }
Original file line number Diff line number Diff line change 1+ // This sample program demonstrates how to create race
2+ // conditions in our programs. We don't want to do this.
3+ package main
4+
5+ import (
6+ "fmt"
7+ "runtime"
8+ "sync"
9+ )
10+
11+ var (
12+ // counter is a variable incremented by all goroutines.
13+ counter = 0
14+
15+ // wg is used to wait for the program to finish.
16+ wg sync.WaitGroup
17+ )
18+
19+ // main is the entry point for all Go programs.
20+ func main () {
21+ // Add a count of two, one for each goroutine.
22+ wg .Add (2 )
23+
24+ // Create two goroutines.
25+ go incCounter (1 )
26+ go incCounter (2 )
27+
28+ // Wait for the goroutines to finish.
29+ wg .Wait ()
30+ fmt .Println ("Final Counter:" , counter )
31+ }
32+
33+ // incCounter increments the package level counter variable.
34+ func incCounter (id int ) {
35+ // Schedule the call to Done to tell main we are done.
36+ defer wg .Done ()
37+
38+ for count := 0 ; count < 2 ; count ++ {
39+ // Capture the value of Counter.
40+ value := counter
41+
42+ // Yield the thread and be placed back in queue.
43+ runtime .Gosched ()
44+
45+ // Increment our local value of Counter.
46+ value ++
47+
48+ // Store the value back into Counter.
49+ counter = value
50+ }
51+ }
Original file line number Diff line number Diff line change 1+ // This sample program demonstrates how to use the atomic
2+ // package to provide safe access to numeric types.
3+ package main
4+
5+ import (
6+ "fmt"
7+ "runtime"
8+ "sync"
9+ "sync/atomic"
10+ )
11+
12+ var (
13+ // counter is a variable incremented by all goroutines.
14+ counter int64
15+
16+ // wg is used to wait for the program to finish.
17+ wg sync.WaitGroup
18+ )
19+
20+ // main is the entry point for all Go programs.
21+ func main () {
22+ // Add a count of two, one for each goroutine.
23+ wg .Add (2 )
24+
25+ // Create two goroutines.
26+ go incCounter (1 )
27+ go incCounter (2 )
28+
29+ // Wait for the goroutines to finish.
30+ wg .Wait ()
31+
32+ // Display the final value.
33+ fmt .Println ("Final Counter:" , counter )
34+ }
35+
36+ // incCounter increments the package level counter variable.
37+ func incCounter (id int ) {
38+ // Schedule the call to Done to tell main we are done.
39+ defer wg .Done ()
40+
41+ for count := 0 ; count < 2 ; count ++ {
42+ // Safely Add One To Counter.
43+ atomic .AddInt64 (& counter , 1 )
44+
45+ // Yield the thread and be placed back in queue.
46+ runtime .Gosched ()
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ // This sample program demonstrates how to use the atomic
2+ // package functions Store and Load to provide safe access
3+ // to numeric types.
4+ package main
5+
6+ import (
7+ "fmt"
8+ "sync"
9+ "sync/atomic"
10+ "time"
11+ )
12+
13+ var (
14+ // shutdown is a flag to alert running goroutines to shutdown.
15+ shutdown int64 = 0
16+
17+ // wg is used to wait for the program to finish.
18+ wg sync.WaitGroup
19+ )
20+
21+ // main is the entry point for all Go programs.
22+ func main () {
23+ // Add a count of two, one for each goroutine.
24+ wg .Add (2 )
25+
26+ // Create two goroutines.
27+ go doWork ("A" )
28+ go doWork ("B" )
29+
30+ // Give the goroutines time to run.
31+ time .Sleep (1 * time .Second )
32+
33+ // Safely flag it is time to shutdown.
34+ fmt .Println ("Shutdown Now" )
35+ atomic .StoreInt64 (& shutdown , 1 )
36+
37+ // Wait for the goroutines to finish.
38+ wg .Wait ()
39+ }
40+
41+ // doWork simulates a goroutine performing work and
42+ // checking the Shutdown flag to terminate early.
43+ func doWork (name string ) {
44+ // Schedule the call to Done to tell main we are done.
45+ defer wg .Done ()
46+
47+ for {
48+ fmt .Printf ("Doing %s Work\\ n" , name )
49+ time .Sleep (250 * time .Millisecond )
50+
51+ // Do we need to shutdown.
52+ if atomic .LoadInt64 (& shutdown ) == 1 {
53+ fmt .Printf ("Shutting %s Down\\ n" , name )
54+ break
55+ }
56+ }
57+ }
Original file line number Diff line number Diff line change 1+ // This sample program demonstrates how to use a mutex
2+ // to define critical sections of code that need synchronous
3+ // access.
4+ package main
5+
6+ import (
7+ "fmt"
8+ "runtime"
9+ "sync"
10+ )
11+
12+ var (
13+ // counter is a variable incremented by all goroutines.
14+ counter int
15+
16+ // wg is used to wait for the program to finish.
17+ wg sync.WaitGroup
18+
19+ // mutex is used to define a critical section of code.
20+ mutex sync.Mutex
21+ )
22+
23+ // main is the entry point for all Go programs.
24+ func main () {
25+ // Add a count of two, one for each goroutine.
26+ wg .Add (2 )
27+
28+ // Create two goroutines.
29+ go incCounter (1 )
30+ go incCounter (2 )
31+
32+ // Wait for the goroutines to finish.
33+ wg .Wait ()
34+ fmt .Printf ("Final Counter: %d\\ n" , counter )
35+ }
36+
37+ // incCounter increments the package level Counter variable
38+ // using the Mutex to synchronize and provide safe access.
39+ func incCounter (id int ) {
40+ // Schedule the call to Done to tell main we are done.
41+ defer wg .Done ()
42+
43+ for count := 0 ; count < 2 ; count ++ {
44+ // Only allow one goroutine through this
45+ // critical section at a time.
46+ mutex .Lock ()
47+
48+ // Capture the value of counter.
49+ value := counter
50+
51+ // Yield the thread and be placed back in queue.
52+ runtime .Gosched ()
53+
54+ // Increment our local value of counter.
55+ value ++
56+
57+ // Store the value back into counter.
58+ counter = value
59+
60+ // Release the lock and allow any
61+ // waiting goroutine through.
62+ mutex .Unlock ()
63+ }
64+ }
You can’t perform that action at this time.
0 commit comments