3

I'm trying to understand one of the Golang typical data races where access to an unprotected global variable from multiple goroutines may cause a race condition:

var service map[string]net.Addr

func RegisterService(name string, addr net.Addr) {
  service[name] = addr
}

func LookupService(name string) net.Addr {
  return service[name]
}

It goes on to say that we can solve this by protecting it with a mutex:

var (
  service   map[string]net.Addr
  serviceMu sync.Mutex
)

func RegisterService(name string, addr net.Addr) {
  serviceMu.Lock()
  defer serviceMu.Unlock()
  service[name] = addr
}

func LookupService(name string) net.Addr {
  serviceMu.Lock()
  defer serviceMu.Unlock()
  return service[name]
}

So far, so good. What's confusing me is this:

The accepted answer to this question suggests that a CPU-bound goroutine will any starve other goroutines that have been multiplexed onto the same OS thread (unless we explicitly yield with runtime.Gosched()). Which makes sense.

To me, the RegisterService() and LookupService() functions above look to be CPU bound, as there's no IO and no yield. Is this correct?

If it is, and if GOMAXPROCS is set to 1, then is a mutex in the above example still strictly necessary? Doesn't the fact that the goroutines are CPU-bound at the point where race conditions might occur take care of it?

Even if it does, I presume that in real life using a mutex is here is still a good idea as we may not be able to guarantee what GOMAXPROCS is set to. Are there another reasons?

5
  • The fact that the Go routines are CPU bound does not mean that there is no chance of sheduling happening inbetween. The Mutex is still neccessary.
    – fuz
    Sep 28, 2013 at 11:58
  • Thanks. So a CPU-bound goroutine might starve other goroutines on the same OS thread, or it might not. We can't guarantee it either way. If so, do you know what factors determine whether a goroutines starves others or not? Sep 28, 2013 at 12:22
  • 6
    A CPU Bound goroutine used to starve others on the same thread but Go 1.2 (almost released) has a pre-emptive scheduler so that is no longer the case. It was only ever an implementation detail IMHO. (In go 1.2 every function call is an opportunity for pre-emption so if your CPU bound function doesn't call other functions it will starve others on the same thread). Sep 28, 2013 at 12:45
  • 9
    @AlexEdwards If your code makes any assumptions about the behavior of the scheduler, it exploits undefined behavior. Don't write such a code. Assume the scheduler might switch between Goroutines at every point in your code.
    – fuz
    Sep 28, 2013 at 13:47
  • @NickCraig-Wood you could probably post that as an answer.
    – Tyler
    Oct 19, 2013 at 7:19

1 Answer 1

0

As FUZxxl and Nick Craig-Wood noted, current behavior of goroutines is implementation-specific. So, maybe, read or write to map can yield. Considering that maps are not thread safe, mutex or another syncronization is required for correct concurrent access.

As alternative to mutex, you can spawn a goroutine, that performs all operations on your map and talk to it with channels:

type writereq struct {
    key string
    value net.Addr
    reply chan struct{}
}

type readreq struct {
    key string
    reply chan net.Addr
}

var service map[string]net.Addr
var reads = make(chan readreq)
var writes = make(chan writereq)

func RegisterService(name string, addr net.Addr) {
    w := writereq{name, addr, make(chan struct{})}
    writes <- w
    return <-w.reply // return after registration confirmation
}

func LookupService(name string) net.Addr {
    r := readreq{name, make(chan net.Addr)}
    reads <- r
    return <-r.reply
}

func serveRegistry() {
    for {
        select {
        case r := <-reads:
            r.reply <- service[r.name]
        case w := <-writes:
            service[w.name] = w.addr
            w.reply <- struct{}
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.