我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

原子AddUint32溢出

I'm using the below code to get unique IDs within process:

for i := 0; i < 10; i++ {
    go func() {
        for {
            atomic.AddUint32(&counter, 1)
            time.Sleep(time.Millisecond)
        }
    }()
}

What will happen if the counter value overflows uint32's limit?

The value wraps around, which is very easy to demonstrate:

u := uint32(math.MaxUint32)
fmt.Println(u)
u++
fmt.Println(u)


// or
u = math.MaxUint32
atomic.AddUint32(&u, 1)
fmt.Println(u)

https://play.golang.org/p/lCOM3nMYNc

近期文章

  • 最大浮线宽度
  • 将CURL转换为Golang代码
  • Golang Unmarshal切片的int切片
  • Golang,在Linux中加载Windows DLL
  • 如何使用sqlx进行LEFT JOIN查询?
  • 非常零星的Go HTTP错误:多个响应。WriteHeader调用
  • 在IntelliJ中导入Go项目不起作用
  • 解析Github的access_token响应
  • 如何在golang中找到(2,3或可能的话n)维切片的尺寸并验证其是否为矩阵?
  • 为什么goroutines没有`.join`操作?
  • Go具有模板语言吗?
  • 面向对象的实现
  • 去将reflect.Value从字符串转换为int
  • 错误的GET页面请求
  • 如何解决函数中的Go数组大小
  • 如何编写插入golang代码?
  • 将unicode字符存储在golang中
  • goroutine或多线程在golang中不起作用
  • 如何在Go中格式化此字符串
  • Archlinux:golang不能使用go build

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华