Skip to content

Commit

Permalink
Enclosure
Browse files Browse the repository at this point in the history
  • Loading branch information
baudekin committed Feb 25, 2024
1 parent 5e56931 commit 156587a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 02-enclosure/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"runtime"
"sync"
)

var wg sync.WaitGroup

func main() {
runtime.GOMAXPROCS(1) // Not needed

// Show I gets get reference by the go routine
var i = 10
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 1000; j++ {
i = i + 1
}
fmt.Printf("Inside go routine i: %d\n", i)
}()

var k = 20
wg.Add(1)
go func(k int) {
defer wg.Done()
for j := 0; j < 1000; j++ {
k = k + 1
}
fmt.Printf("Inside go routine k: %d\n", k)
}(k)

// Danager Ahead two go routines accessing the same memory location at the same time
// semgrp looks for this condition DON'T do this!!!
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 1000; j++ {
i = i + 1
}
fmt.Printf("Inside second go routine i: %d\n", i)
}()

wg.Wait()
fmt.Printf("Outside go routine i: %d\n", i)
fmt.Printf("Outside go routine k: %d\n", k)

}

0 comments on commit 156587a

Please sign in to comment.