Sync!
Last active
August 29, 2015 14:17
-
-
Save shogo82148/9ed20d46560ff116d604 to your computer and use it in GitHub Desktop.
golang sync test
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
l := new(sync.Mutex) | |
c := sync.NewCond(l) | |
for i := 0; i < 10; i++ { | |
go func(i int) { | |
fmt.Printf("waiting %d\n", i) | |
l.Lock() | |
defer l.Unlock() | |
c.Wait() | |
fmt.Printf("go %d\n", i) | |
}(i) | |
} | |
for i := 3; i >= 0; i-- { | |
time.Sleep(1 * time.Second) | |
fmt.Println(i) | |
} | |
c.Broadcast() | |
time.Sleep(3 * 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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
l := new(sync.Mutex) | |
c := sync.NewCond(l) | |
for i := 0; i < 10; i++ { | |
go func(i int) { | |
fmt.Printf("waiting %d\n", i) | |
l.Lock() | |
defer l.Unlock() | |
c.Wait() | |
fmt.Printf("go %d\n", i) | |
}(i) | |
} | |
for i := 0; i < 10; i++ { | |
time.Sleep(1 * time.Second) | |
c.Signal() | |
} | |
time.Sleep(1 * 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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
wg := new(sync.WaitGroup) | |
wg,Add(2) | |
go func() { | |
wg.Done() | |
}() | |
go func() { | |
} | |
} |
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func parallel(wg *sync.WaitGroup, m *sync.Mutex) { | |
m.Lock() | |
defer m.Unlock() | |
fmt.Println("博") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("多") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("の") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("塩") | |
wg.Done() | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
wg := new(sync.WaitGroup) | |
m := new(sync.Mutex) | |
for i := 0; i < 3; i++ { | |
wg.Add(1) | |
go parallel(wg, m) | |
} | |
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func parallel(wg *sync.WaitGroup) { | |
fmt.Println("博") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("多") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("の") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("塩") | |
wg.Done() | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
wg := new(sync.WaitGroup) | |
for i := 0; i < 3; i++ { | |
wg.Add(1) | |
go parallel(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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
) | |
var once = new(sync.Once) | |
func greeting(wg *sync.WaitGroup) { | |
once.Do(func() { | |
fmt.Println("こんにちわ") | |
}) | |
fmt.Println("ごきげんいかがですか") | |
wg.Done() | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
defer fmt.Println("さようなら") | |
wg := new(sync.WaitGroup) | |
for i := 0; i < 5; i++ { | |
wg.Add(1) | |
go greeting(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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
p := sync.Pool{ | |
New: func() interface{} { | |
return "定時作業" | |
}, | |
} | |
wg := new(sync.WaitGroup) | |
for i := 0; i < 5; i++ { | |
p.Put("割込作業") | |
time.Sleep(100 * time.Millisecond) | |
} | |
runtime.GC() | |
wg.Add(1) | |
go func() { | |
for i := 0; i < 10; i++ { | |
fmt.Println(p.Get()) | |
time.Sleep(50 * time.Millisecond) | |
} | |
wg.Done() | |
}() | |
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
p := sync.Pool{ | |
New: func() interface{} { | |
return "定時作業" | |
}, | |
} | |
wg := new(sync.WaitGroup) | |
wg.Add(1) | |
go func() { | |
for i := 0; i < 10; i++ { | |
p.Put("割込作業") | |
time.Sleep(100 * time.Millisecond) | |
} | |
wg.Done() | |
}() | |
wg.Add(1) | |
go func() { | |
for i := 0; i < 10; i++ { | |
fmt.Println(p.Get()) | |
time.Sleep(50 * time.Millisecond) | |
} | |
wg.Done() | |
}() | |
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func writeParallel(wg *sync.WaitGroup, m *sync.RWMutex) { | |
m.Lock() | |
defer m.Unlock() | |
fmt.Println("博") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("多") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("の") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("塩") | |
wg.Done() | |
} | |
func readParallel(wg *sync.WaitGroup, m *sync.RWMutex) { | |
m.RLock() | |
defer m.RUnlock() | |
fmt.Println("博") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("多") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("の") | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("塩") | |
wg.Done() | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
wg := new(sync.WaitGroup) | |
m := new(sync.RWMutex) | |
fmt.Println("Read vs Read") | |
wg.Add(2) | |
go readParallel(wg, m) | |
go readParallel(wg, m) | |
wg.Wait() | |
fmt.Println("") | |
fmt.Println("Write vs Write") | |
wg.Add(2) | |
go writeParallel(wg, m) | |
go writeParallel(wg, m) | |
wg.Wait() | |
fmt.Println("") | |
fmt.Println("Write vs Read") | |
wg.Add(2) | |
go writeParallel(wg, m) | |
go readParallel(wg, m) | |
wg.Wait() | |
fmt.Println("") | |
} |
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
package main | |
import ( | |
"fmt" | |
"runtime" | |
"sync" | |
"time" | |
) | |
func sleep(wg *sync.WaitGroup, number int) { | |
time.Sleep(time.Duration(number) * time.Second) | |
fmt.Println(number) | |
wg.Done() | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
items := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5} | |
wg := new(sync.WaitGroup) | |
for _, i := range items { | |
wg.Add(1) | |
go sleep(wg, i) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment