Skip to content

questions about the pillTimer lock in the processPill function in Step 10 #46

@kyzyc

Description

@kyzyc

In Step 10, if the first goroutine is blocking while waiting for data from the pillTimer.C channel, and a second goroutine enters and stops the previous pillTimer, the first goroutine will never receive data from the pillTimer.C channel and will remain blocked until the program terminates. Although this locking mechanism in Step 10 ensures functional correctness, I wonder if this situation is still somewhat unsatisfactory.

var pillTimer *time.Timer
var pillMx sync.Mutex

func processPill() {
	pillMx.Lock()
	updateGhosts(ghosts, GhostStatusBlue)
	if pillTimer != nil {
		pillTimer.Stop()
	}
	pillTimer = time.NewTimer(time.Second * time.Duration(cfg.PillDurationSecs))
	pillMx.Unlock()
	<-pillTimer.C
	pillMx.Lock()
	pillTimer.Stop()
	updateGhosts(ghosts, GhostStatusNormal)
	pillMx.Unlock()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions