登录
首页 >  Golang >  Go问答

安全并发使用的旋转计数器

来源:stackoverflow

时间:2024-02-28 22:36:24 336浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《安全并发使用的旋转计数器》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我想写这样的东西:

type RoundRobinList struct {
    lst []string
    idx uint32
}

// rr_list is a RoundRobinList
func loop(rr_list) {
   start = rr_list.idx
   rr_list.idx = (rr_list.idx + 1)%len(rr_list.lst)
   print(rr_list.lst[idx])
}

如果 rr_list.lst = ["a", "b", "c"], 并且循环被一遍又一遍地调用,我期望打印以下内容:

“一个” “b” “c” “a” “b” “c”...

这安全吗? rr_list.idx = (rr_list.idx + 1)%len(rr_list.lst)


解决方案


每当您读取和写入未以某种方式保护的值(使用互斥体或 sync 包中的其他一些东西)时,您就会遇到竞争,这意味着您无法使用它安全地处于并发设置中。

在这里,您正在读取和写入 roundrobinlist 结构的 idx 字段,没有任何保护,因此如果您同时使用它,就会发生竞争。

作为第一道防线,您应该了解内存安全规则并仔细遵循它们,除非您非常确定代码是安全的,否则不要编写代码。作为第二道防线,您可以使用竞争检测器来发现许多并发访问缺乏安全性的问题。

这是一个简单的测试用例,位于文件 a_test.go 中。我还必须修复代码中的一些错误才能使其编译。它启动两个 goroutine,每个 goroutine 在共享的 roundrobinlist 上调用 loop 1000 次。

package main

import (
    "sync"
    "testing"
)

type roundrobinlist struct {
    lst []string
    idx uint32
}

func loop(rr *roundrobinlist) {
    rr.idx = (rr.idx + 1) % uint32(len(rr.lst))
}

func testloop(t *testing.t) {
    rr := roundrobinlist{[]string{"a", "b", "c"}, 0}
    var wg sync.waitgroup
    wg.add(2)
    for n := 0; n < 2; n++ {
        go func() {
            for i := 0; i < 1000; i++ {
                loop(&rr)
            }
            wg.done()
        }()
    }
    wg.wait()
}

使用 go test -race ./a_test.go 运行结果:

==================
WARNING: DATA RACE
Read at 0x00c0000b0078 by goroutine 9:
  command-line-arguments.loop()
      /mnt/c/Users/paul/Desktop/a_test.go:14 +0xa1
  command-line-arguments.TestLoop.func1()
      /mnt/c/Users/paul/Desktop/a_test.go:24 +0x78

Previous write at 0x00c0000b0078 by goroutine 8:
  command-line-arguments.loop()
      /mnt/c/Users/paul/Desktop/a_test.go:14 +0x4e
  command-line-arguments.TestLoop.func1()
      /mnt/c/Users/paul/Desktop/a_test.go:24 +0x78

Goroutine 9 (running) created at:
  command-line-arguments.TestLoop()
      /mnt/c/Users/paul/Desktop/a_test.go:22 +0x1cc
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:992 +0x1eb

Goroutine 8 (finished) created at:
  command-line-arguments.TestLoop()
      /mnt/c/Users/paul/Desktop/a_test.go:22 +0x1cc
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:992 +0x1eb
==================
--- FAIL: TestLoop (0.00s)
    testing.go:906: race detected during execution of test
FAIL
FAIL    command-line-arguments  0.006s
FAIL

本篇关于《安全并发使用的旋转计数器》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>