Variable values will be the same between goroutines.

Asked 2 years ago, Updated 2 years ago, 45 views

Nice to meet you.
I am currently studying go, and I am studying goroutine.
I put some string keys in the map as below, and the value is flagged with bool.
If the value is true, I would like to apply sleep processing, and if it is false, I would like to apply sleep processing without sleep.
However, if you do the following, some key values of the map will be covered.
When I check the address of the variable, sometimes it is the same address, but why is it the same address?
Also, how can I output all the values in the map so that I don't wear them?

package main

import(
    "fmt"
    "time"
)

func goroutine (name string) {
    for {
        select{
        case<-sleep:
            fmt.Printf("----------------(sleep) name=%s, name of address=%p\n", name, & name)
            time.Sleep(5*time.Second)
            fmt.Printf("----------------- wake up name=%s", name)
        case<-start:
            fmt.Printf("------------------(start) name=%s, name of address=%p\n", name, & name)
        }
    }
}

var sleep chan bool
var start chan bool

funcmain(){
    sleep=make(chanbool)
    start=make(chanbool)

    names: = make (map [string])bool)
    names["a"] = false
    names["b"] = false
    names["c"] = false
    names["d"] = true
    names["e"] = false
    fork, v: = range names {
        if v{
            go goroutine(k)
            sleep<-true
        } else{
            go goroutine(k)
            start<-true
        }
    }

    time.Sleep (1000*time.Second)
}
#go runmain.go
--------------- (start) name = a, name of address = 0xc00006e1c0
--------------- (start) name=c, name of address=0xc0000a2000
--------------- (sleep) name=c, name of address=0xc0000a2000
--------------- (start) name=e, name of address=0xc0000a2020
--------------- (start) name = a, name of address = 0xc00006e1c0
------------------- wake up name =c

go goroutine

2022-09-30 19:32

1 Answers

The for in the goroutine function continues to spin until the time.Sleep(1000*time.Second) of main is over, so
After passing case<-start: in the first loop, go through case<-start: or case<-sleep: in the next loop, depending on the timing.

Except for for, it will be similar to the intended behavior, but if you only want sleep when d, it will be ↓.

package main

import(
    "fmt"
    "sync"
    "time"
)

func goroutine (name string, isSleep bool) {
    ifisSleep{
        fmt.Printf("----------------(sleep) name=%s, name of address=%p\n", name, & name)
        time.Sleep(5*time.Second)
        fmt.Printf("----------------- wake up name=%s\n", name)
    } else{
        fmt.Printf("------------------(start) name=%s, name of address=%p\n", name, & name)
    }
}

funcmain(){
    names: = map [string]US>bool{
        "a"—false,
        "b"—false,
        "c"—false,
        "d"—true,
        "e"—false,
    }

    varwgsync.WaitGroup
    // If the execution multiplicity is set
    semaphore: = make (chan int, 2)
    fork, v: = range names {
        wg.Add(1)
        go func(kstring, vbool){
            defer wg. Done()
            semaphore <-1
            goroutine(k,v)
            <-semaphore
        }(k,v)
    }

    wg.Wait()
}


2022-09-30 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.