osthrpool

package module
v0.0.0-...-36443d3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2015 License: BSD-2-Clause Imports: 4 Imported by: 0

README

osthrpool GoDoc

Package osthrpool provides a pool of locked OS threads that grows and shrinks automatically and distribute the load over the threads.

Example:

	// Make a new pool of 5 goroutines that are locked to an OS thread.
	// These goroutines are automatically unlocked after 500ms if there
	// are no more tasks to execute.
	p := osthrpool.New(5, 500*time.Millisecond)

	// Execute 10 tasks.
	for i := 0; i < 10; i++ {
		fmt.Println("index:", i)

		p.Execute(func() {
			fmt.Println("task: ", i)
		})
	}

	// Output:
	// index: 0
	// task:  0
	// index: 1
	// task:  1
	// index: 2
	// task:  2
	// index: 3
	// task:  3
	// index: 4
	// task:  4
	// index: 5
	// task:  5
	// index: 6
	// task:  6
	// index: 7
	// task:  7
	// index: 8
	// task:  8
	// index: 9
	// task:  9

Documentation

Overview

Package osthrpool provides a pool of locked OS threads.

Example
// Make a new pool of 5 goroutines that are locked to an OS thread.
// These goroutines are automatically unlocked after 500ms if there
// are no more tasks to execute.
p := New(5, 500*time.Millisecond)

// Execute 10 tasks.
for i := 0; i < 10; i++ {
	fmt.Println("index:", i)

	p.Execute(func() {
		fmt.Println("task: ", i)
	})
}
Output:

index: 0
task:  0
index: 1
task:  1
index: 2
task:  2
index: 3
task:  3
index: 4
task:  4
index: 5
task:  5
index: 6
task:  6
index: 7
task:  7
index: 8
task:  8
index: 9
task:  9

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {

	// InitFn is called by a worker just before it starts the work loop. The
	// goroutine is locked to an OS thread when InitFn is called.
	InitFn func()

	// ExitFn is called by a worker just before it exits. The goroutine is still
	// locked to an OS thread when ExitFn is called.
	ExitFn func()
	// contains filtered or unexported fields
}

Pool represents a pool of locked OS threads.

func New

func New(maxSize int, timeout time.Duration) *Pool

New returns a pool of locked OS threads that grows to it's maximum size and shrinks automatically depending on the load. A thread automatically unlocks itself after a timeout if there are no more tasks to process.

func (*Pool) Execute

func (p *Pool) Execute(t Task)

Execute executes the given task on a locked OS thread.

type Task

type Task func()

Task represents a task that requires to be run on a locked OS thread.

Jump to

Keyboard shortcuts

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