{"id":335792,"date":"2022-07-17T03:00:08","date_gmt":"2022-07-17T03:00:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=335792"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=335792","title":{"rendered":"<span>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 sync\/atomic \u0432 golang \u0434\u043b\u044f float64<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0432\u0441\u0435\u043c \u0433\u043e\u0444\u0435\u0440\u0430\u043c! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u043c <code>sync\/atomic<\/code> \u0434\u043b\u044f \u0442\u0438\u043f\u0430 float64.<\/p>\n<h3>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438<\/h3>\n<p>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u0430\u043d\u0430\u043b \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0444\u043b\u0430\u0433 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0431\u044b\u043b\u0430 \u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 (\u0442\u0438\u043f float64). \u041d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0441\u0443\u043c\u043c\u0443 \u0432\u0441\u0435\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0448\u0438\u0431\u043e\u043a. <\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c sync.Mutex<\/h3>\n<p>\u0417\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>sync.Mutex<\/code>:<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"math\" \"sync\" \"sync\/atomic\" )  const countWorkers = 1000 const countTasks = 10000000  type Result struct { value    float64 hasError bool }  func MakeTasks(count int) &lt;-chan Result { ch := make(chan Result) go func() { for i := 0; i &lt; count; i++ { ch &lt;- Result{ value:    float64(i) * 2.42, hasError: (i % 10) == 0, }  } close(ch) }() return ch }  func ProcessUsingMutex(ch &lt;-chan Result, countWorkers int) (float64, int64) { var wg sync.WaitGroup var errMu sync.Mutex var mu sync.Mutex var countErrors int64 var result float64  for i := 0; i &lt; countWorkers; i++ { wg.Add(1) go func() { for item := range ch { if item.hasError { errMu.Lock() countErrors++ errMu.Unlock() } else { mu.Lock() result += item.value mu.Unlock() } } wg.Done() }() } wg.Wait() return result, countErrors }   func main() { ch := MakeTasks(countTasks) fmt.Println(ProcessUsingMutex(ch, countWorkers)) } <\/code><\/pre>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c sync\/atomic<\/h3>\n<p>\u041d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>sync.Mutex<\/code> \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0442\u044c \u043d\u0430\u0448\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>sync\/atomic<\/code>.  \u0414\u043b\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0412\u043c\u0435\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>sync.Mutex<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>atomic.AddInt64<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c:<\/p>\n<pre><code class=\"go\">errMu.Lock() countErrors++ errMu.Unlock()<\/code><\/pre>\n<p>\u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430<\/p>\n<pre><code class=\"go\">atomic.AddInt64(&amp;countErrors, 1)<\/code><\/pre>\n<p>\u041d\u043e \u0434\u043b\u044f float64 \u043d\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>AddFloat64<\/code>. \u041d\u043e \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>AddInt64<\/code> \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>CompareAndSwapInt64<\/code><\/p>\n<pre><code class=\"go\">func AddInt64(addr *int64, delta int64) (new int64) { for { if v := *addr; atomic.CompareAndSwapInt64(addr, v, v+delta) { return v } } }<\/code><\/pre>\n<p>\u041d\u043e \u0434\u043b\u044f <code>float64<\/code> \u043d\u0435\u0442 \u0438 <code>CompareAndSwap<\/code>, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>float64<\/code> \u0432 <code>uint64<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f  <code>math.Float64bits<\/code> \u0438 \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u0436\u0435 <code>uint64<\/code>.<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>AddFloat64<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u043e\u0439:<\/p>\n<pre><code class=\"go\">func AddFloat64(addr *uint64, delta float64) uint64 { for { cur := atomic.LoadUint64(addr) curVal := math.Float64frombits(cur) nxtVal := curVal + delta nxt := math.Float64bits(nxtVal) if atomic.CompareAndSwapUint64(addr, cur, nxt) { return nxt } } }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 <code>AddFloat64 <\/code>\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u044e\u0442\u0435\u043a\u0441\u043e\u0432:<\/p>\n<pre><code class=\"go\">func ProcessUsingAtomic(ch &lt;-chan Result, countWorkers int) (float64, int64) { var wg sync.WaitGroup var countErrors int64 var total uint64 for i := 0; i &lt; countWorkers; i++ { wg.Add(1) go func() { for item := range ch { if item.hasError { atomic.AddInt64(&amp;countErrors, 1) } else { AddFloat64(&amp;total, item.value) } } wg.Done() }() } wg.Wait() return math.Float64frombits(atomic.LoadUint64(&amp;total)), atomic.LoadInt64(&amp;countErrors) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f mutex \u0438 atomic \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a<\/p>\n<pre><code class=\"go\">package main  import ( \"testing\" )  func BenchmarkProcessUsingAtomic(b *testing.B) {  for i := 0; i &lt; b.N; i++ { b.StopTimer() ch := MakeTasks(countTasks) b.StartTimer() ProcessUsingAtomic(ch, countWorkers) } }  func BenchmarkProcessUsingMutex(b *testing.B) { for i := 0; i &lt; b.N; i++ { b.StopTimer() ch := MakeTasks(countTasks) b.StartTimer() ProcessUsingMutex(ch, countWorkers) } }  <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a \u0434\u0430\u043d\u043d\u043e\u0433\u043e benchmark \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0447\u0442\u043e atomic \u043d\u0430 5-7% \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<blockquote>\n<p>BenchmarkProcessUsingAtomic-8                  1        2468118459 ns\/op<br \/> BenchmarkProcessUsingMutex-8                   1        2640532917 ns\/op<br \/> PASS<\/p>\n<\/blockquote>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041c\u043e\u0434\u0443\u043b\u044c <code>sync\/atomic<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u043c\u044e\u0442\u0435\u043a\u0441\u043e\u0432 \u0442\u0430\u043a \u043a\u0430\u043a \u0430\u0442\u043e\u043c\u0438\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043c\u044e\u0442\u0435\u043a\u0441\u043e\u0432. \u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0442\u043e\u043c\u0438\u043a\u0438 \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 <code>sync\/atomic<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430, \u043a\u0430\u043a \u044f \u043e\u043f\u0438\u0441\u0430\u043b \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438\u043b\u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 <code>Value<\/code> \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f  <code>sync\/atomic<\/code>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/677332\/\"> https:\/\/habr.com\/ru\/post\/677332\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0432\u0441\u0435\u043c \u0433\u043e\u0444\u0435\u0440\u0430\u043c! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u043c <code>sync\/atomic<\/code> \u0434\u043b\u044f \u0442\u0438\u043f\u0430 float64.<\/p>\n<h3>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438<\/h3>\n<p>\u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u0430\u043d\u0430\u043b \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0447\u0438\u0442\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0444\u043b\u0430\u0433 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0431\u044b\u043b\u0430 \u043b\u0438 \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 (\u0442\u0438\u043f float64). \u041d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0441\u0443\u043c\u043c\u0443 \u0432\u0441\u0435\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0448\u0438\u0431\u043e\u043a. <\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c sync.Mutex<\/h3>\n<p>\u0417\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>sync.Mutex<\/code>:<\/p>\n<pre><code class=\"go\">package main  import ( \"fmt\" \"math\" \"sync\" \"sync\/atomic\" )  const countWorkers = 1000 const countTasks = 10000000  type Result struct { value    float64 hasError bool }  func MakeTasks(count int) &lt;-chan Result { ch := make(chan Result) go func() { for i := 0; i &lt; count; i++ { ch &lt;- Result{ value:    float64(i) * 2.42, hasError: (i % 10) == 0, }  } close(ch) }() return ch }  func ProcessUsingMutex(ch &lt;-chan Result, countWorkers int) (float64, int64) { var wg sync.WaitGroup var errMu sync.Mutex var mu sync.Mutex var countErrors int64 var result float64  for i := 0; i &lt; countWorkers; i++ { wg.Add(1) go func() { for item := range ch { if item.hasError { errMu.Lock() countErrors++ errMu.Unlock() } else { mu.Lock() result += item.value mu.Unlock() } } wg.Done() }() } wg.Wait() return result, countErrors }   func main() { ch := MakeTasks(countTasks) fmt.Println(ProcessUsingMutex(ch, countWorkers)) } <\/code><\/pre>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c sync\/atomic<\/h3>\n<p>\u041d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>sync.Mutex<\/code> \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0442\u044c \u043d\u0430\u0448\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>sync\/atomic<\/code>.  \u0414\u043b\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e. \u0412\u043c\u0435\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>sync.Mutex<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>atomic.AddInt64<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c:<\/p>\n<pre><code class=\"go\">errMu.Lock() countErrors++ errMu.Unlock()<\/code><\/pre>\n<p>\u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430<\/p>\n<pre><code class=\"go\">atomic.AddInt64(&amp;countErrors, 1)<\/code><\/pre>\n<p>\u041d\u043e \u0434\u043b\u044f float64 \u043d\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>AddFloat64<\/code>. \u041d\u043e \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c. \u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>AddInt64<\/code> \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>CompareAndSwapInt64<\/code><\/p>\n<pre><code class=\"go\">func AddInt64(addr *int64, delta int64) (new int64) { for { if v := *addr; atomic.CompareAndSwapInt64(addr, v, v+delta) { return v } } }<\/code><\/pre>\n<p>\u041d\u043e \u0434\u043b\u044f <code>float64<\/code> \u043d\u0435\u0442 \u0438 <code>CompareAndSwap<\/code>, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>float64<\/code> \u0432 <code>uint64<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f  <code>math.Float64bits<\/code> \u0438 \u043e\u043f\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u0436\u0435 <code>uint64<\/code>.<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <code>AddFloat64<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u043e\u0439:<\/p>\n<pre><code class=\"go\">func AddFloat64(addr *uint64, delta float64) uint64 { for { cur := atomic.LoadUint64(addr) curVal := math.Float64frombits(cur) nxtVal := curVal + delta nxt := math.Float64bits(nxtVal) if atomic.CompareAndSwapUint64(addr, cur, nxt) { return nxt } } }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 <code>AddFloat64 <\/code>\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u044e\u0442\u0435\u043a\u0441\u043e\u0432:<\/p>\n<pre><code class=\"go\">func ProcessUsingAtomic(ch &lt;-chan Result, countWorkers int) (float64, int64) { var wg sync.WaitGroup var countErrors int64 var total uint64 for i := 0; i &lt; countWorkers; i++ { wg.Add(1) go func() { for item := range ch { if item.hasError { atomic.AddInt64(&amp;countErrors, 1) } else { AddFloat64(&amp;total, item.value) } } wg.Done() }() } wg.Wait() return math.Float64frombits(atomic.LoadUint64(&amp;total)), atomic.LoadInt64(&amp;countErrors) }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f mutex \u0438 atomic \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a<\/p>\n<pre><code class=\"go\">package main  import ( \"testing\" )  func BenchmarkProcessUsingAtomic(b *testing.B) {  for i := 0; i &lt; b.N; i++ { b.StopTimer() ch := MakeTasks(countTasks) b.StartTimer() ProcessUsingAtomic(ch, countWorkers) } }  func BenchmarkProcessUsingMutex(b *testing.B) { for i := 0; i &lt; b.N; i++ { b.StopTimer() ch := MakeTasks(countTasks) b.StartTimer() ProcessUsingMutex(ch, countWorkers) } }  <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a \u0434\u0430\u043d\u043d\u043e\u0433\u043e benchmark \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0447\u0442\u043e atomic \u043d\u0430 5-7% \u0431\u044b\u0441\u0442\u0440\u0435\u0435.<\/p>\n<blockquote>\n<p>BenchmarkProcessUsingAtomic-8                  1        2468118459 ns\/op<br \/> BenchmarkProcessUsingMutex-8                   1        2640532917 ns\/op<br \/> PASS<\/p>\n<\/blockquote>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u041c\u043e\u0434\u0443\u043b\u044c <code>sync\/atomic<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u043c\u044e\u0442\u0435\u043a\u0441\u043e\u0432 \u0442\u0430\u043a \u043a\u0430\u043a \u0430\u0442\u043e\u043c\u0438\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043c\u044e\u0442\u0435\u043a\u0441\u043e\u0432. \u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0442\u043e\u043c\u0438\u043a\u0438 \u0434\u043b\u044f \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 <code>sync\/atomic<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430, \u043a\u0430\u043a \u044f \u043e\u043f\u0438\u0441\u0430\u043b \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438\u043b\u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 <code>Value<\/code> \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f  <code>sync\/atomic<\/code>.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/677332\/\"> https:\/\/habr.com\/ru\/post\/677332\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-335792","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=335792"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335792\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}