Golang atomic count, mutex, time-consuming

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
" Sync "  "sync/atomic " "Time  " " Runtime "

1.runtime. Gosched () said to let the CPU to the time chip to others, the next time continue to resume execution of the goroutine, oneself is generally blocked, this is a very advanced sleep, we often encounter the question of how long to sleep, there is no need to consider, When someone else finishes, you will be notified naturally.

2.var Mutex sync. The mutex defines a mutex variable, uses atomic for the related atomic operation, and requires read access to the manipulated variable to Lock() mutex ensure exclusive access to the variable , and after the operation completes Unlock() the mutex, To ensure that the Go process does not die in the schedule, we explicitly use it for release after each operation runtime.Gosched() , and this release is generally handled automatically.

3. Record run time run start with t: = times. Now () records the start time, which is used at the end of the program. Now (). Sub (t) to get total time-consuming (1.XXXS), which is typically used in the code start of Main ()

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.