세마포어는 있는지 모르겠다만 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]
'Programming > golang' 카테고리의 다른 글
golang http.request (0) | 2022.09.14 |
---|---|
golang range (0) | 2022.09.06 |
golang make와 new (0) | 2022.09.06 |
golang defer 와 panic(), recover() (0) | 2022.09.06 |
go 루틴 (0) | 2022.09.06 |