pool

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2014 License: GPL-3.0-or-later Imports: 3 Imported by: 13

Documentation

Overview

Example (ThreadPool)

Usage example for the thread pool.

package main

import (
	"fmt"
	"time"

	"github.com/project-iris/iris/pool"
)

func main() {
	// Create a new thread pool with 5 concurrent worker capacity
	workers := pool.NewThreadPool(5)

	// Start the pool (you could schedule tasks before starting, and they would
	// wait queued until permission is given to execute)
	workers.Start()

	// Schedule some tasks (functions with no arguments nor return values)
	for i := 0; i < 10; i++ {
		id := i // Need to copy i for the task closure
		workers.Schedule(func() {
			time.Sleep(time.Duration(id) * 50 * time.Millisecond)
			fmt.Printf("Task #%d done.\n", id)
		})
	}
	// Terminate the pool gracefully (don't clear unstarted tasks)
	workers.Terminate(false)

}
Output:

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

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrTerminating = errors.New("pool terminating")

Functions

This section is empty.

Types

type Task

type Task func()

A task function meant to be started as a go routine.

type ThreadPool

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

A thread pool to place a hard limit on the number of go-routines doing some type of (possibly too consuming) work.

func NewThreadPool

func NewThreadPool(cap int) *ThreadPool

Creates a thread pool with the given concurrent thread capacity.

func (*ThreadPool) Clear

func (t *ThreadPool) Clear()

Dumps the waiting tasks from the pool.

func (*ThreadPool) Schedule

func (t *ThreadPool) Schedule(task Task) error

Schedules a new task into the thread pool.

func (*ThreadPool) Start

func (t *ThreadPool) Start()

Starts the thread pool and workers.

func (*ThreadPool) Terminate

func (t *ThreadPool) Terminate(clear bool)

Waits for all threads to finish, terminating the whole pool afterwards. No new tasks are accepted in the meanwhile.

Jump to

Keyboard shortcuts

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