Skip to content

listing7.14 Receive early c.capacitySema.Release() call? #13

@alsoLut

Description

@alsoLut

I guess a more correct impl would be:

func (c *Channel[M]) Receive() M {
    c.sizeSema.Acquire()
    c.mutex.Lock()
    v := c.buffer.Remove(c.buffer.Front()).(M)
    c.mutex.Unlock()
    c.capacitySema.Release()
    return v
}

In the original implementation, if 100 goroutines call Receive() on an empty channel, all of them execute the first line capacitySema.Release() but then block at sizeSema.Acquire(). If then 100 Send() calls occur, they would all fill the buffer unchecked. Right?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions