Search code examples
gothread-safetygoroutine

How to run single instance of goroutine


I have a goroutine that will be run multiple times. But it can only run one at a time (single instance). What is the correct/idiomatic way to make sure a certain goroutine can run only one at a time?

Here is my contrived example code to illustrate the point:

func main() {
    // Contrived example!!!!!!
    // theCaller() may be run at multiple, unpredictable times
    // theJob() must only be run one at a time
    go theCaller()
    go theCaller()
    go theCaller()
}

func theCaller() {
    if !jobIsRunning { // race condition here!
        jobIsRunning = true
        go theJob()
    }
}

var jobIsRunning bool

// Can run multiple times, but only one at a time
func theJob() {
    defer jobDone()
    do_something()
}

func jobDone() {
    jobIsRunning = false
}

Solution

  • Based on question and other comments from the OP, it looks like the goal is to start a new job if and only if a job is not already running.

    Use a boolean variable protected by a sync.Mutex to record the running state of of the job. Set the variable to true when starting a job and to false when the job completes. Test this variable to determine if a job should be started.

    var (
        jobIsRunning   bool
        JobIsrunningMu sync.Mutex
    )
    
    func maybeStartJob() {
        JobIsrunningMu.Lock()
        start := !jobIsRunning
        jobIsRunning = true
        JobIsrunningMu.Unlock()
        if start {
            go func() {
                theJob()
                JobIsrunningMu.Lock()
                jobIsRunning = false
                JobIsrunningMu.Unlock()
            }()
        }
    }
    
    func main() {
        maybeStartJob()
        maybeStartJob()
        maybeStartJob()
    }
    

    The lower-level sync/atomic package can also be used and may have better performance than using a mutex.

    var jobIsRunning uint32
    
    func maybeStartJob() {
        if atomic.CompareAndSwapUint32(&jobIsRunning, 0, 1) {
            go func() {
                theJob()
                atomic.StoreUint32(&jobIsRunning, 0)
            }()
        }
    }
    

    The sync/atomic package documentation warns that the functions in the package require great care to use correctly and that most applications should use the sync package.