Skip to content

A Safe Tomb Package - a safe way to do lifecycle management of goroutines

License

Notifications You must be signed in to change notification settings

milandamen/tomb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Safe Tomb Package

This package provides a safe way to do lifecycle management of goroutines.

In contrast to the more well-known tomb package, this package has:

Example

package example

import (
	"fmt"
	"github.com/milandamen/tomb"
	"time"
)

func DoWork() error {
	var t tomb.Tomb
	err := t.Go(func() {
		timer := time.NewTimer(time.Second)
		for {
			select {
			case <-timer.C:
				fmt.Println("hello world 1")
			case <-t.Dying():
				return
			}
		}
	})
	if err != nil {
		return err
	}

	err = t.Go(func() {
		timer := time.NewTimer(time.Second)
		for {
			select {
			case <-timer.C:
				fmt.Println("hello world 2")
			case <-t.Dying():
				return
			}
		}
	})
	if err != nil {
		return err
	}

	// Do other work
	time.Sleep(5 * time.Second)

	// Stop the goroutines using the tomb.
	t.Kill()
	err = t.Wait(2 * time.Second)
	if err != nil {
		return err
	}

	return nil
}

License

This project is licensed under the terms of the MIT license.

About

A Safe Tomb Package - a safe way to do lifecycle management of goroutines

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages