go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/simutil/wait_all.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package simutil
     9  
    10  import (
    11  	"fmt"
    12  	"sync"
    13  )
    14  
    15  // WaitAll blocks on all of the event sources completing.
    16  func WaitAll(sources ...Waiter) {
    17  	fmt.Printf("joining %d sources\n", len(sources))
    18  	sem := new(sync.WaitGroup)
    19  	sem.Add(len(sources))
    20  	for _, source := range sources {
    21  		go func(s Waiter) {
    22  			defer func() { sem.Done() }()
    23  			s.Wait()
    24  		}(source)
    25  	}
    26  	sem.Wait()
    27  	fmt.Printf("joined %d sources\n", len(sources))
    28  }