concurrency

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2022 License: BSD-3-Clause Imports: 3 Imported by: 0

README

USAGE

package main

import (
	"fmt"
	"time"

	"github.com/liran/concurrency"
)

func main() {
	// Create a thread pool that can allocate up to 10 threads
	pool := concurrency.New(10, func(params ...interface{}) {
		n := params[0].(int)
		w := params[1].(string)
		time.Sleep(time.Second)
		fmt.Println(w, n)
	})
	defer pool.Close()

	for i := 0; i < 100; i++ {
		fmt.Println("a:", i)
		pool.Process(i, "hello")
	}
	pool.Wait()

	for i := 0; i < 20; i++ {
		fmt.Println("b:", i)
		pool.Process(i, "world")
	}

	// Multiple threads will be notified at the same time
	go func() {
		pool.Wait()
		fmt.Println("wait on go thread ")
	}()

	pool.Wait()
	fmt.Println("wait on main thread")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

func New

func New(threads int, worker func(params ...interface{})) *Pool

func (*Pool) Close

func (c *Pool) Close()

func (*Pool) Process

func (c *Pool) Process(params ...interface{})

func (*Pool) Wait

func (c *Pool) Wait()

Jump to

Keyboard shortcuts

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