当在for循环内修改了一个公共变量后,我无法看到变化。

huangapple go评论81阅读模式
英文:

When accessing a public var that's been modified inside a for loop, i cannot see a change

问题

我有一个名为Tpus的公共变量,在一个无限的for循环中修改这个值,但是当我尝试在for循环中打印Tpus和在for循环外的goroutine中打印Tpus时,得到了两个不同的结果。我希望在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(string=136.144.49.213:8004) %!e(string=198.55.56.164:8004)]
  • [%!e(string=198.55.56.164:8004) %!e(string=13.124.174.97:8003)]
  • [%!e(string=13.124.174.97:8003) %!e(string=185.16.38.73:8003)]
  • [%!e(string=185.16.38.73:8003) %!e(string=103.28.52.250:8003)]
  • [%!e(string=103.28.52.250:8003) %!e(string=18.214.103.198:8004)]
  • [%!e(string=18.214.103.198:8004) %!e(string=185.188.42.43:9003)]
  • [%!e(string=185.188.42.43:9003) %!e(string=135.181.115.253:8003)]
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(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
英文:

I have a public variable which we call Tpus, I modify this value within an infinite for loop, however i get 2 different results when i try to print Tpus in the for loop, and in a goroutine outside the for loop, What im looking to do is get the same result in the goroutine as i get in the for loop.

    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(&quot;\n + %e&quot;, Tpus)
            }
        }()
        for {
            slot := GetSlot(destination)
            slotleaders := GetSlotLeaders(destination, strconv.FormatUint(slot+10, 10))
            for x := 0; x &lt; 80; x++ {
                for z := 0; z &lt; len(clusternodes.Result); z++ {
                    if slotleaders[x] == clusternodes.Result[z].Pubkey {
                        if len(Tpus) &gt;= 2 {
                            X := RemoveIndex(Tpus, 0)
                            Tpus = append(X, clusternodes.Result[x].Tpu)
                            fmt.Printf(&quot;\n + %e&quot;, Tpus)
                        } else {
                            Tpus = append(Tpus, clusternodes.Result[x].Tpu)
                        }
                    }
                }
            }
        }
    }

Result From Code Above:

  • [%!e(string=136.144.49.213:8004) %!e(string=198.55.56.164:8004)]
  • [%!e(string=198.55.56.164:8004) %!e(string=13.124.174.97:8003)]
  • [%!e(string=13.124.174.97:8003) %!e(string=185.16.38.73:8003)]
  • [%!e(string=185.16.38.73:8003) %!e(string=103.28.52.250:8003)]
  • [%!e(string=103.28.52.250:8003) %!e(string=18.214.103.198:8004)]
  • [%!e(string=18.214.103.198:8004) %!e(string=185.188.42.43:9003)]
  • [%!e(string=185.188.42.43:9003) %!e(string=135.181.115.253:8003)]
    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(&quot;\n + %e&quot;, Tpus)
            }
        }()
        for {
            slot := GetSlot(destination)
            slotleaders := GetSlotLeaders(destination, strconv.FormatUint(slot+10, 10))
            for x := 0; x &lt; 80; x++ {
                for z := 0; z &lt; len(clusternodes.Result); z++ {
                    if slotleaders[x] == clusternodes.Result[z].Pubkey {
                        if len(Tpus) &gt;= 2 {
                            X := RemoveIndex(Tpus, 0)
                            Tpus = append(X, clusternodes.Result[x].Tpu)
                            //fmt.Printf(&quot;\n + %e&quot;, Tpus)
                        } else {
                            Tpus = append(Tpus, clusternodes.Result[x].Tpu)
                        }
                    }
                }
            }
        }
    }

Result From Code Above:

  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]
  • [%!e(string=142.132.248.119:8003) %!e(string=3.231.94.117:8004)]

答案1

得分: 1

var globalvar []string

func main() {
    var mu sync.Mutex

    go func() {
        for {
            // 阻止对全局变量的访问,以便
            // 没有人可以在 goroutine 之外更改它。
            // 如果在 goroutine 之外已经锁定,
            // 则等待解锁。
            mu.Lock()

            // 对全局变量进行一些操作...
            fmt.Printf("%v\n", globalvar)

            // 解锁对全局变量的访问
            mu.Unlock()

            // 一些代码...
        }
    }()

    for i := 0; i < 255; i++ {
        // 阻止对全局变量的访问。
        // 如果在 goroutine 内部已经锁定,
        // 则等待解锁。
        mu.Lock()

        // 对全局变量进行一些操作
        globalvar = append(globalvar, "Str #"+strconv.Itoa(i))

        // 解锁访问
        mu.Unlock()

        // 一些代码...
    }
}

你还可以定义一个带有互斥锁、值和用于更改值的方法的特殊结构。类似这样:


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))
    }
}

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

英文:
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&#39;s already locked outside the goroutine,
            // then wait for unlocking.
            mu.Lock()

            // Some actions with a global variable...
            fmt.Printf(&quot;%v\n&quot;, globalvar)

            // Unlocking access to a global variable
            mu.Unlock()

            // Some code...
        }
    }()

    for i := 0; i &lt; 255; i++ {
        // Block access to a global variable.
        // If it&#39;s already locked inside the goroutine,
        // then wait for unlocking.
        mu.Lock()

        // Some actions with a global variable
        globalvar = append(globalvar, &quot;Str #&quot;+strconv.Itoa(i))

        // Unlock access
        mu.Unlock()

        // Some code...
    }
}

Also you can define a special structure with mutex, value and methods for change it value. Something like this:


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(&quot;%v&quot;, t.value)
}

var Tpus TpusContainer

func main() {
    go func() {
        for {
            fmt.Printf(&quot;%v\n&quot;, Tpus)
        }
    }()

    for i := 0; i &lt; 255; i++ {
        Tpus.RemoveIndex(0)
        Tpus.Append(&quot;Some string #&quot;+strconv.Itoa(i))
    }
}

Personally, I prefer the second approach.

huangapple
  • 本文由 发表于 2022年4月28日 02:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/72033425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定