Go Atomic count

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Post a reply

By atomic counting, the same numeric value can be added and reduced in multi-threaded cases, which is generally used for state synchronization.
Look at the code first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
Package Main

Import"FMT"
Import"Time"
Import"Sync/atomic"
Import"Runtime"

Func Main() {

//define an integer
var opsUInt64 = 0

//Use 50 threads to accumulate values for OPS
forI:= 0;I< -;I++ {
Go func() {
for {
//Add 1 per time
Atomic.AddUint64(&Ops,1)

//This function is used for time slice switching
//Can be understood as the premium version of time. Sleep ()
//Avoid the previous for loop to put the CPU time slice in one thread, so that other threads do not have the opportunity to execute
Runtime.gosched()
}
}()
}

//Stop for one second, the top 50 threads have 1 seconds of execution time
Time.Sleep( Time.Second)

//Get Results
Opsfinal:=Atomic.LoadUint64(&Ops)
Fmt.Println("Ops:",Opsfinal)
}

The printing results are similar:
ops:40200

If you do not use an atomic count, using the OPS =ops+1 directly results in a multi-threaded count that is inaccurate.
Open go source in the atomic package, you can see the relevant algorithms are written in assembly language. So the atomic count performs very efficiently.

This entry was posted on October 6, 2015. belongs to the Go language category. The author is Baiyuxiong.

Article Navigation

←Earlier articles than new articles →

Search this site article

Log classification

    • Go Language (76)
    • Swift (10)
    • webOS Development (8)
    • Messy (59)
    • Programming (170)
    • Website Production (72)

Latest Articles

    • Remember a weird win10 "lost" file event
    • Enterprises can not find the mobile phone number of the enterprise's solution
    • Gradle Study Summary
    • About strong references in Swift weak references no primary references
    • Connection to AWS EC2 tips operation timed out solution

Recent comments

    • The fishing device is published in the "Twitter-like open source micro-blogging system"
    • Electronic Fishing device published in the "Twitter-like open-source micro-blogging system"
    • Fishing Machine published in "Twitter-like open-source micro-blogging system"
    • The Electric Fish Machine is published in "Twitter-like open source micro-blogging system"
    • The fishing device is published in the "Twitter-like open source micro-blogging system"

Log Index

Log Index

Link table

    • imcn-, I'm a rookie.
    • Chinese decoration
    • Chinese decoration
    • Code Maniac
    • Temple decoration
    • Nutshell Blog

Meta

    • Login
    • Article RSS
    • Comment RSS
    • WordPress.org
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.