waitcontext

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2019 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package waitcontext implements a mechanism for making a context's CancelFunc wait for goroutines to finish.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"htdvisser.dev/exp/waitcontext"
)

func main() {
	// Create a context that waits after cancel:
	ctx, cancelAndWait := waitcontext.New(context.Background())

	// Start a goroutine:
	waitcontext.Go(ctx, func() {
		<-ctx.Done()
		time.Sleep(10 * time.Millisecond)
		fmt.Println("Really done")
	})

	// You could extend the context elsewhere in your program:
	ctx, _ = waitcontext.New(ctx)

	// Start another goroutine:
	waitcontext.Go(ctx, func() {
		<-ctx.Done()
		time.Sleep(time.Millisecond)
		fmt.Println("Really done")
	})

	// Cancel the context and wait until all goroutines started in the context (or derived contexts) are done:
	cancelAndWait()

}
Output:

Really done
Really done

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(ctx context.Context, f func())

Go starts a new goroutine for f, and makes any CancelAndWaitFunc created for (a parent of) ctx wait for it to finish.

Types

type CancelAndWaitFunc

type CancelAndWaitFunc func()

CancelAndWaitFunc cancels the context and waits for all

func New

New returns a new context derived from parent, and a function that cancels the context and waits until all goroutines started with waitcontext.Go have finished.

Jump to

Keyboard shortcuts

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