From 3eafb89c33b46824f0c03973b7ced532b9df4cb1 Mon Sep 17 00:00:00 2001 From: Nathan North Date: Sun, 14 Apr 2024 10:15:28 -0700 Subject: [PATCH] Buzz12: Sync primative for stop all (#13) --- hive.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/hive.go b/hive.go index fbc6baa..7adb34b 100644 --- a/hive.go +++ b/hive.go @@ -12,6 +12,7 @@ type Hive struct { notifyComplete chan struct{} middleware []MiddleFunc closed chan struct{} + lock sync.Mutex } // New initializes a new [*Hive]. @@ -35,6 +36,8 @@ func (hive *Hive) startCleanupWorker() { } func (hive *Hive) removeDoneWorkers() { + hive.lock.Lock() + defer hive.lock.Unlock() finished := []int{} for i := range hive.colony { if hive.colony[i].done.Load() { @@ -71,10 +74,16 @@ func (hive *Hive) Submit(workers ...*Worker) { // StopAll should only be used when you are completely done with the hive. Internal channels are // closed and all workers are shutdown. func (hive *Hive) StopAll() { - for i := range hive.colony { - hive.colony[i].Stop() - } + hive.stopAll() hive.block.Wait() close(hive.notifyComplete) <-hive.closed } + +func (hive *Hive) stopAll() { + hive.lock.Lock() + defer hive.lock.Unlock() + for i := range hive.colony { + hive.colony[i].Stop() + } +}