如果写操作在设计上从未相互竞争,使用sync.RWMutex的读锁进行写操作并使用其写锁进行读操作是否安全?

From the Go docs:

A RWMutex is a reader/writer mutual exclusion lock. The lock can be held by an arbitrary number of readers or a single writer.

This introductory sentence is never followed by a definition of what a reader and writer may and may not do. Thus, I wonder whether a certain stretch of the definition is possible.

Suppose we have a set of many goroutines; let's call it S. Each of the goroutines in S has its own resources which it reads from and writes to frequently. Suppose I have one extra goroutine R which routinely wants to scrape the state of the resources written by the goroutines in S. Let's explore the two more obvious solutions to see where I am going.

  1. We can use a single mutex. However, the goroutines in S would needlessly compete for the lock, since each goroutine from S accesses only its own resources anyway.
  2. Use one mutex for each goroutine in S. This eliminates needless competition and even gives R the option to decide whether it needs to have locked all mutexex at once or merely each mutex at least once at some point in time. Requires some extra work however.

So my third idea is this: Let the goroutines in S take the read lock of a sync.RWMutex when they want to read or write (i.e. a generalized inclusive lock) and let R take the write lock of that mutex when it wants to read (i.e. a generalized exclusive lock). In other words:

Is it safe to use the read lock of sync.RWMutex for writing and its write lock for reading if the writes never race with each other by design?

The answer of your question is Yes, just like Volker said.

I would give you a more idiomatic Go solution:

every resources of goroutines in S take a sync.RWMutex
the goroutines in S read after sync.RWMutex.Rlock()
the goroutines in S write after sync.RWMutex.Lock()
extra goroutine R read after sync.RWMutex.Rlock()

I think you just go into the wrong region, it should be a simple problem. :)

If I misunderstand you, tell me.

Answering myself because of all the confusion.

Yes, it is safe to use the read lock of sync.RWMutex for writing and its write lock for reading if the writes never race with each other.

Example:

package main

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

func main() {
    m := &sync.RWMutex{}
    wg := &sync.WaitGroup{}
    maxWriters := 5
    wg.Add(maxWriters+1)
    allData := make([]uint64, maxWriters)
    go showAccumulatedData(m, wg, allData)
    for i := 0; i < maxWriters; i++ {
        go writeData(m, wg, &allData[i])
    }
    wg.Wait()
    fmt.Println(accumulateData(m, allData))
}

func writeData(m *sync.RWMutex, wg *sync.WaitGroup, d *uint64) {
    for i := 0; i < 1000; i++ {
        m.RLock()
        *d++ // Write during read-lock.
        m.RUnlock()
        time.Sleep(time.Millisecond)
    }
    wg.Done()
}

func showAccumulatedData(m *sync.RWMutex, wg *sync.WaitGroup, allData []uint64) {
    for i := 0; i < 15; i++ {
        fmt.Println(accumulateData(m, allData))
        time.Sleep(time.Millisecond*50)
    }
    wg.Done()
}

func accumulateData(m *sync.RWMutex, allData []uint64) uint64 {
    var accumulator uint64
    m.Lock()
    for _, v := range allData {
        accumulator += v // Read during write-lock.
    }
    m.Unlock()
    return  accumulator
}