Popularity
1.6
Growing
Activity
0.0
Stable
15
3
3

Programming language: Go
License: MIT License
Tags: Goroutines    

go-tools/multithreading alternatives and similar packages

Based on the "Goroutines" category.
Alternatively, view go-tools/multithreading alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of go-tools/multithreading or a related project?

Add another 'Goroutines' Package

README

go-tools

A collection of tools for Golang, focusing on concurrency and goroutines

Godoc Go Report Card Build Status License

The [multithreading](multithreading) library currently supports a ThreadTracker struct that allows you to easily manage goroutines.

  • Create new goroutines.
  • Wait for all goroutines to finish.
  • Set deferred functions to be executed after goroutines finish.
  • Easily handle panics inside goroutines with a panic handler.
  • Stop the threadTracker from receiving new functions.
  • Fetch the number of currently active goroutines.

Install

Install the package with:

go get github.com/nikhilsaraf/go-tools/multithreading

Import it with:

import "github.com/nikhilsaraf/go-tools/multithreading"

and use multithreading as the package name inside the code.

Example

package main

import (
  "fmt"
  "github.com/nikhilsaraf/go-tools/multithreading"
)

func main() {
  // create thread tracker instance
  threadTracker := multithreading.MakeThreadTracker()

  // start thread functions
  for i := 0; i < 10; i++ {
    err := threadTracker.TriggerGoroutine(func(inputs []interface{}) {
      // pass `i` as a value to the goroutine and read from `inputs`.
      // this is needed to "bind" the variable to this goroutine.
      value := inputs[0].(int)

      fmt.Printf("Goroutine #%d\n", value)
    }, []interface{}{i})

    if err != nil {
      panic(err)
    }
  }

  // wait for all threads to finish
  threadTracker.Wait()
  fmt.Printf("done\n")
}

Sample Output:

Goroutine #1
Goroutine #2
Goroutine #9
Goroutine #0
Goroutine #3
Goroutine #7
Goroutine #6
Goroutine #4
Goroutine #8
Goroutine #5
done

Test Examples

thread_tracker_test.go


*Note that all licence references and agreements mentioned in the go-tools/multithreading README section above are relevant to that project's source code only.