goroutine

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2024 License: MIT Imports: 3 Imported by: 0

README

GoDoc

goroutine

A Go package that provides a simple way to manage goroutines and facilitate their graceful shutdown.

import "github.com/blizzy78/goroutine"

Code example

// This function does the actual work.
// In this example, we're not using the Context, but you really always should.
worker := func(_ context.Context) {
	time.Sleep(100 * time.Millisecond)
}

goroutines := New()

// Start a new goroutine.
goroutines.Go(context.Background(), worker)

// Cancel all goroutines' contexts, and wait for them to finish.
_ = goroutines.CancelAll(context.Background(), true)

License

This package is licensed under the MIT license.

Documentation

Overview

Package goroutine provides a simple way to manage goroutines and facilitate their graceful shutdown.

Example
// This function does the actual work.
// In this example, we're not using the Context, but you really always should.
worker := func(_ context.Context) {
	time.Sleep(100 * time.Millisecond)
}

goroutines := New()

// Start a new goroutine.
goroutines.Go(context.Background(), worker)

// Cancel all goroutines' contexts, and wait for them to finish.
_ = goroutines.CancelAll(context.Background(), true)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goroutines

type Goroutines struct {
	// contains filtered or unexported fields
}

Goroutines manages a collection of goroutines.

func New

func New() *Goroutines

New creates a new Goroutines.

func (*Goroutines) AwaitTermination

func (gs *Goroutines) AwaitTermination(ctx context.Context) error

AwaitTermination waits for all goroutines to finish. Returns ctx.Err() if ctx is canceled while waiting, or nil otherwise.

func (*Goroutines) CancelAll

func (gs *Goroutines) CancelAll(ctx context.Context, awaitTermination bool) error

CancelAll cancels all goroutines' contexts. If awaitTermination==true, waits for all goroutines to finish. Returns ctx.Err() if ctx is canceled while waiting, or nil otherwise.

func (*Goroutines) Go

func (gs *Goroutines) Go(ctx context.Context, fun func(ctx context.Context))

Go starts fun in a new goroutine. This works basically the same as the language's go keyword.

Jump to

Keyboard shortcuts

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