From 5a0032801ff84f8e963778c154b436c4f9e33ae7 Mon Sep 17 00:00:00 2001 From: jacobKdev Date: Fri, 4 Aug 2023 12:05:09 +0100 Subject: [PATCH] fix issue with cond --- channel/channel.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/channel/channel.go b/channel/channel.go index 7d5582f..ec55bc0 100644 --- a/channel/channel.go +++ b/channel/channel.go @@ -25,12 +25,12 @@ func NewChannel(bufferSize int) *Channel { } func (ch *Channel) Send(value interface{}) { - ch.Mutex.Lock() - defer ch.Mutex.Unlock() + ch.Cond.L.Lock() for len(ch.buffer) == ch.bufferSize && !ch.closed { ch.Cond.Wait() } + ch.Cond.L.Unlock() if ch.closed { panic("Sending on closed channel") @@ -42,12 +42,11 @@ func (ch *Channel) Send(value interface{}) { } func (ch *Channel) Receive() (interface{}, bool) { - ch.Mutex.Lock() - defer ch.Mutex.Unlock() - + ch.Cond.L.Lock() for len(ch.buffer) == 0 && !ch.closed { ch.Cond.Wait() } + ch.Cond.L.Unlock() if len(ch.buffer) == 0 && ch.closed { return nil, false