Programming/golang
golang mutex
구차니
2022. 9. 6. 16:54
세마포어는 있는지 모르겠다만 mutex는 존재하네
package main import ( "fmt" "sync" "time" ) // SafeCounter is safe to use concurrently. type SafeCounter struct { mu sync.Mutex v map[string]int } // Inc increments the counter for the given key. func (c *SafeCounter) Inc(key string) { c.mu.Lock() // Lock so only one goroutine at a time can access the map c.v. c.v[key]++ c.mu.Unlock() } // Value returns the current value of the counter for the given key. func (c *SafeCounter) Value(key string) int { c.mu.Lock() // Lock so only one goroutine at a time can access the map c.v. defer c.mu.Unlock() return c.v[key] } |
[링크 : https://go.dev/tour/concurrency/9]
관련 검색어로 golang mutex vs channel 이라는게 나오는데 channel이 그렇게 빠르지는 않은 듯.
[링크 : http://www.dogfootlife.com/archives/452]