-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added golang examples: mutex, wg, timer, ticker. And some about inter…
…faces
- Loading branch information
Showing
6 changed files
with
154 additions
and
11 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
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ package main | |
|
||
import ("fmt" | ||
// "runtime" | ||
"errors" | ||
// "errors" | ||
"time") | ||
|
||
|
||
|
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" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
|
||
func addSome(add1 *int, add2 *int32, mu *sync.Mutex) { | ||
mu.Lock() | ||
*add1 = *add1 + 1 | ||
mu.Unlock() | ||
|
||
atomic.AddInt32(add2, 1) | ||
} | ||
|
||
func main() { | ||
var some = 0 | ||
var some_atomic int32 = 10 | ||
|
||
mu := &sync.Mutex{} | ||
|
||
for i := 1; i <= 1000; i++ { | ||
go addSome(&some, &some_atomic, mu) | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
fmt.Println(some, some_atomic) | ||
} | ||
|
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,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func sleep(t time.Duration, wg *sync.WaitGroup) { | ||
fmt.Println("Sleep: ", t) | ||
time.Sleep(t) | ||
fmt.Println("Sleep: ", t) | ||
wg.Done() | ||
} | ||
|
||
func main() { | ||
wg := &sync.WaitGroup{} | ||
|
||
wg.Add(1) | ||
go sleep(1 * time.Second, wg) | ||
|
||
wg.Add(1) | ||
go sleep(2 * time.Second, wg) | ||
|
||
wg.Add(1) | ||
go sleep(3 * time.Second, wg) | ||
|
||
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,32 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func someWithTimer(t *time.Timer, q chan int) { | ||
time.Sleep(1 * time.Second) | ||
select { | ||
case <- t.C: | ||
fmt.Println("Time out") | ||
case <- q: | ||
if !t.Stop() { | ||
<- t.C | ||
} | ||
fmt.Println("Timer was stopped") | ||
default: | ||
fmt.Println("End function") | ||
} | ||
} | ||
|
||
|
||
|
||
func main() { | ||
timer := time.NewTimer(1 * time.Second) | ||
quit := make(chan int) | ||
|
||
go someWithTimer(timer, quit) | ||
|
||
time.Sleep(2 * time.Second) | ||
} |
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" | ||
"time" | ||
) | ||
|
||
|
||
func main() { | ||
ticker := time.NewTicker(1 * time.Second) | ||
|
||
count := 0 | ||
for tick := range ticker.C { | ||
count++ | ||
fmt.Printf("Super tick #%v, time %v\n", count, tick) | ||
if count > 4 { | ||
ticker.Stop() | ||
break | ||
} | ||
} | ||
|
||
} |