chanutil

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2023 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Killer

type Killer struct {
	// C is a channel to receive the kill signal. It is safe with multiple
	// receive operations from multiple goroutines.
	//
	// Technically, C is just closed when Kill is called. Note that reading
	// from closed channels never gets blocked.
	C <-chan struct{}
	// contains filtered or unexported fields
}

Killer sends a "kill" signal to goroutines.

Example
package main

import (
	"fmt"
	"sync"

	"github.com/eanavitarte/webpackager/internal/chanutil"
)

func main() {
	k := chanutil.NewKiller()

	var wg sync.WaitGroup
	wg.Add(3)

	for i := 0; i < 3; i++ {
		go func(i int, k *chanutil.Killer) {
			defer wg.Done()
			<-k.C // Wait for the kill signal.
			fmt.Printf("Worker %v is killed\n", i)
		}(i, k)
	}

	k.Kill()

	wg.Wait()
}
Output:

Worker 0 is killed
Worker 1 is killed
Worker 2 is killed

func NewKiller

func NewKiller() *Killer

NewKiller returns a new Killer.

func (Killer) Kill

func (k Killer) Kill()

Kill notifies k of the kill signal. Technically, Kill closes k.C to unblock all receive operations.

Kill detects the past kill signal and avoids closing k.C multiple times, but is unsafe for concurrent calls since the detection and closure is not atomic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL