为了账号安全,请及时绑定邮箱和手机立即绑定

访问在 for 循环内修改的公共变量时,我看不到变化

访问在 for 循环内修改的公共变量时,我看不到变化

Go
UYOU 2022-12-05 17:26:04
我有一个我们称之为 Tpus 的公共变量,我在无限循环中修改了这个值,但是当我尝试在 for 循环中打印 Tpus 以及在 for 循环外的 goroutine 中打印 Tpus 时,我得到了 2 个不同的结果,我在寻找什么做的是在 goroutine 中得到与我在 for 循环中得到的结果相同的结果。    var Tpus []string        func TPU_Calc(destination string) {        clusternodes := GetClusterNodes(destination)        go func() {            wg.Add(1)            time.Sleep(2 * time.Second)            for {                time.Sleep(150 * time.Millisecond)                //fmt.Printf("\n + %e", Tpus)            }        }()        for {            slot := GetSlot(destination)            slotleaders := GetSlotLeaders(destination, strconv.FormatUint(slot+10, 10))            for x := 0; x < 80; x++ {                for z := 0; z < len(clusternodes.Result); z++ {                    if slotleaders[x] == clusternodes.Result[z].Pubkey {                        if len(Tpus) >= 2 {                            X := RemoveIndex(Tpus, 0)                            Tpus = append(X, clusternodes.Result[x].Tpu)                            fmt.Printf("\n + %e", Tpus)                        } else {                            Tpus = append(Tpus, clusternodes.Result[x].Tpu)                        }                    }                }            }        }    }上面代码的结果:[%!e(字符串=136.144.49.213:8004)%!e(字符串=198.55.56.164:8004)][%!e(字符串=198.55.56.164:8004)%!e(字符串=13.124.174.97:8003)][%!e(字符串=13.124.174.97:8003) %!e(字符串=185.16.38.73:8003)][%!e(字符串=185.16.38.73:8003) %!e(字符串=103.28.52.250:8003)][%!e(字符串=103.28.52.250:8003) %!e(字符串=18.214.103.198:8004)][%!e(字符串=18.214.103.198:8004)%!e(字符串=185.188.42.43:9003)][%!e(字符串=185.188.42.43:9003)%!e(字符串=135.181.115.253:8003)]
查看完整描述

1 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

var globalvar []string


func main() {

    var mu sync.Mutex


    go func() {

        for {

            // Block access to a global variable, so that

            // no one can change it outside the goroutine.

            // If it's already locked outside the goroutine,

            // then wait for unlocking.

            mu.Lock()


            // Some actions with a global variable...

            fmt.Printf("%v\n", globalvar)


            // Unlocking access to a global variable

            mu.Unlock()


            // Some code...

        }

    }()


    for i := 0; i < 255; i++ {

        // Block access to a global variable.

        // If it's already locked inside the goroutine,

        // then wait for unlocking.

        mu.Lock()


        // Some actions with a global variable

        globalvar = append(globalvar, "Str #"+strconv.Itoa(i))


        // Unlock access

        mu.Unlock()


        // Some code...

    }

}


您还可以定义一个特殊的结构,其中包含互斥体、值和更改其值的方法。是这样的:



type TpusContainer struct {

    mu    sync.Mutex

    value []string

}


func (t *TpusContainer) RemoveIndex(i int) {

    t.mu.Lock()

    defer t.mu.Unlock()

    t.value = RemoveIndex(t.value, i)

}


func (t *TpusContainer) Append(elem string) {

    t.mu.Lock()

    defer t.mu.Unlock()

    t.value = append(t.value, elem)

}


func (t *TpusContainer) String() string {

    t.mu.Lock()

    defer t.mu.Unlock()

    return fmt.Sprintf("%v", t.value)

}


var Tpus TpusContainer


func main() {

    go func() {

        for {

            fmt.Printf("%v\n", Tpus)

        }

    }()


    for i := 0; i < 255; i++ {

        Tpus.RemoveIndex(0)

        Tpus.Append("Some string #"+strconv.Itoa(i))

    }

}


就个人而言,我更喜欢第二种方法。


查看完整回答
反对 回复 2022-12-05
  • 1 回答
  • 0 关注
  • 135 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号