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

go-tools

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

Godoc Go Report Card Build Status License

The 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

Owner
Nikhil Saraf
Donate via Stellar: nikhilsaraf*keybase.io GDKWO57AJ2XCER3NC4NIA2CYCDGVJFBSHT73U5OIBUED3BQLAZU4JJLW
Nikhil Saraf
Similar Resources

Queue is a Golang library for spawning and managing a Goroutine pool

Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing you to create multiple worker according to limit CPU number of machine.

Jan 2, 2023

Simple application that waits for a given time and attempts and then exits

Wait Simple application that waits for a given time and attempts and then exits. WAIT_HOSTS is a list of hosts to wait for. e.g. WAIT_HOSTS=tcp://app:

Nov 24, 2021

Simple in-memory job queue for Golang using worker-based dispatching

artifex Simple in-memory job queue for Golang using worker-based dispatching Documentation here: https://godoc.org/github.com/mborders/artifex Cron jo

Dec 24, 2022

CyclicBarrier golang implementation

cyclicbarrier CyclicBarrier is a synchronizer that allows a set of goroutines to wait for each other to reach a common execution point, also called a

Nov 30, 2022

TryLock support on read-write lock for Golang

go-trylock TryLock support on read-write lock for Golang Interface go-trylock implements sync.Locker. Have same interfaces with sync.RWMutex Documenta

Sep 26, 2022

Fast resizable golang semaphore primitive

semaphore Fast resizable golang semaphore based on CAS allows weighted acquire/release; supports cancellation via context; allows change semaphore lim

Dec 13, 2022

Golang simple thread pool implementation

Golang Threadpool implementation Scalable threadpool implementation using Go to handle the huge network trafic. Install go get github.com/shettyh/thre

Dec 12, 2022

Off heap golang memory pool

Stealthpool stealthpool provides a memory pool that allocates blocks off-heap that will NOT be tracked by the garbage collector. The name stealthpool

Dec 5, 2022

goroutine pool in golang

goroutine pool in golang

Nov 1, 2021
Related tags
Simply way to control goroutines execution order based on dependencies
Simply way to control goroutines execution order based on dependencies

Goflow Goflow is a simply package to control goroutines execution order based on dependencies. It works similar to async.auto from node.js async packa

Dec 8, 2022
Limits the number of goroutines that are allowed to run concurrently

Golang Concurrency Manager Golang Concurrency Manager package limits the number of goroutines that are allowed to run concurrently. Installation Run t

Dec 12, 2022
golang worker pool , Concurrency limiting goroutine pool

golang worker pool 中文说明 Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks su

Dec 19, 2022
This repository collects common concurrency patterns in Golang

Go Concurrency Patterns This repository collects common concurrency patterns in Golang Materials Concurrency is not parallelism Go Concurrency Pattern

Jan 9, 2023
A sync.WaitGroup with error handling and concurrency control

go-waitgroup How to use An package that allows you to use the constructs of a sync.WaitGroup to create a pool of goroutines and control the concurrenc

Dec 31, 2022
Structured Concurrency in Go

nursery: structured concurrency in Go RunConcurrently( // Job 1 func(context.Context, chan error) { time.Sleep(time.Millisecond * 10)

Dec 27, 2022
Concurrency limiting goroutine pool

workerpool Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting task

Dec 28, 2022
Pengenalan Concurrency dan Parallel Programming
Pengenalan Concurrency dan Parallel Programming

Golang Goroutine Sumber Tutorial: Udemy Slide Pengenalan Concurrency dan Parallel Programming Pengenalan Parallel Programming Saat ini kita hidup dima

Nov 5, 2021
Work pool channlege - An url hash retriever worker pool for getting hash digest for a collection of urls

Code challenge The aim of this project is to provide an url hash retriever worke

Feb 16, 2022
Queue is a Golang library for spawning and managing a Goroutine pool

Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing you to create multiple worker according to limit CPU number of machine.

Jan 9, 2023