├── 01.go ├── 02.go ├── 1a.go └── README.md /01.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | const ( 6 | N_INCREMENTS = 10000000 7 | ) 8 | 9 | func main() { 10 | 11 | counter := 0 12 | donechan := make(chan bool) 13 | 14 | go func(done chan<- bool) { 15 | for i := 0; i < N_INCREMENTS; i++ { 16 | counter++ 17 | } 18 | done <- true 19 | }(donechan) 20 | 21 | for i := 0; i < N_INCREMENTS; i++ { 22 | counter++ 23 | } 24 | 25 | _ = <-donechan 26 | 27 | fmt.Println("Count: ", counter) 28 | 29 | } -------------------------------------------------------------------------------- /02.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | ) 7 | 8 | const ( 9 | N_INCREMENTS = 10000000 10 | ) 11 | 12 | func main() { 13 | 14 | counter := 0 15 | donechan := make(chan bool) 16 | var m sync.Mutex 17 | 18 | go func(done chan<- bool) { 19 | for i := 0; i < N_INCREMENTS; i++ { 20 | m.Lock() 21 | counter++ 22 | m.Unlock() 23 | } 24 | done <- true 25 | }(donechan) 26 | 27 | for i := 0; i < N_INCREMENTS; i++ { 28 | m.Lock() 29 | counter++ 30 | m.Unlock() 31 | } 32 | 33 | _ = <-donechan 34 | 35 | fmt.Println("Count: ", counter) 36 | 37 | } -------------------------------------------------------------------------------- /1a.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | ) 7 | 8 | const ( 9 | N_INCREMENTS = 100000000 10 | ) 11 | 12 | func main() { 13 | 14 | counter := 0 15 | donechan := make(chan bool) 16 | var m sync.Mutex 17 | 18 | go func(done chan<- bool) { 19 | for i := 0; i < N_INCREMENTS; i++ { 20 | m.Lock() 21 | counter++ 22 | m.Unlock() 23 | } 24 | done <- true 25 | }(donechan) 26 | 27 | for i := 0; i < N_INCREMENTS; i++ { 28 | m.Lock() 29 | counter++ 30 | m.Unlock() 31 | } 32 | 33 | _ = <-donechan 34 | 35 | 36 | fmt.Println("Counter: ", counter) 37 | 38 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | intro-to-go2 2 | ============ 3 | 4 | This repository contains code snippets discussed in 15-440, lecture 5 (given on 1/28/2014). 5 | 6 | To clone this repository, execute the following command: 7 | 8 | ```sh 9 | git clone https://github.com/cmu440/intro-to-go2 10 | ``` 11 | 12 | To run the file named `01.go`, simply execute: 13 | 14 | ```sh 15 | go run 01.go 16 | ``` 17 | --------------------------------------------------------------------------------