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 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) Schedule ¶
func (t *ThreadPool) Schedule(task Task) error
Schedules a new task into the thread pool.
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.