Giter Site home page Giter Site logo

thread-pool's Introduction

PROBLEM=========================================================================
Thread creation is an 'heavy' process.
Too many thrads hurts performance.

SOLUTION========================================================================
thread-pool is a simple library that allows users for asynchronous computation
using only a fixed number of threads optimizing overall performance.

SAMPLE USAGE===================================================================

CL-USER> (require :thread-pool)

CL-USER> (let ((out *standard-output*))
	   (defun make-lazy-fib (n)
	     (labels ((fib (n) (if (> n 2)
                               (+ (fib (- n 1)) (fib (- n 2)))
			       1)))
	       (lambda ()
		 (format out "~%fib ~a = ~a~%" n (fib n))))))

CL-USER> (defvar *pool* (thread-pool:make-thread-pool 3))
*POOL*

CL-USER> (thread-pool:start-pool *pool*)
(#<SB-THREAD:THREAD "Anonymous thread" RUNNING {1002B88871}>
 #<SB-THREAD:THREAD "Anonymous thread" RUNNING {1002B88A71}>
 #<SB-THREAD:THREAD "Anonymous thread" RUNNING {1002B88C71}>)

CL-USER> (let ((out *standard-output*))
	   (defun make-lazy-fib (n)
	     (flet ((fib (n) (if (> n 2)
				 (+ (fib (- n 1)) (fib (- n 2)))
				 1)))
	       (lambda ()
		 (format out "~%fib ~a = ~a~%" n (fib n))))))
MAKE-LAZY-FIB
CL-USER> (progn (thread-pool:add-to-pool *pool* (make-lazy-fib 15))
		(thread-pool:add-to-pool *pool* (make-lazy-fib 20))
		(thread-pool:add-to-pool *pool* (make-lazy-fib 5))
		(thread-pool:add-to-pool *pool* (make-lazy-fib 17))
		(thread-pool:add-to-pool *pool* (make-lazy-fib 10))
		(thread-pool:add-to-pool *pool* (make-lazy-fib 6)))

(#<CLOSURE (LAMBDA #) {10058664E9}>) 
(#<CLOSURE (LAMBDA #) {10058664E9}> #<CLOSURE (LAMBDA #) {1005867419}>) 
(#<CLOSURE (LAMBDA #) {10058664E9}> #<CLOSURE (LAMBDA #) {1005867419}>
 #<CLOSURE (LAMBDA #) {10058709C9}>) 

fib 15 = 610
(#<CLOSURE (LAMBDA #) {1005867419}> #<CLOSURE (LAMBDA #) {10058709C9}>
 #<CLOSURE (LAMBDA #) {1005872699}>) 
(#<CLOSURE (LAMBDA #) {10058709C9}> #<CLOSURE (LAMBDA #) {1005872699}>
 #<CLOSURE (LAMBDA #) {1005874369}>) 
(#<CLOSURE (LAMBDA #) {10058709C9}> #<CLOSURE (LAMBDA #) {1005872699}>
 #<CLOSURE (LAMBDA #) {1005874369}> #<CLOSURE (LAMBDA #) {1005876039}>) 

fib 5 = 5

fib 20 = 6765

fib 17 = 1597

fib 10 = 55

fib 6 = 8
1

thread-pool's People

Contributors

jonathansmith avatar

Stargazers

Bruno Dias avatar Józef Piątkiewicz avatar  avatar Kyle Nusbaum avatar Corey Girard avatar  avatar  avatar Andrea Chiumenti avatar  avatar Brian O'Reilly avatar  avatar

Watchers

Andrea Chiumenti avatar James Cloos avatar Franklin Tamborello, PhD, CSP, CHFP avatar

thread-pool's Issues

Why not destroy stopped threads?

I noticed thread-pool doesn't seem to destroy the threads it creates. Several cycles of starting and stopping the pool results in a bunch of unused anonymous threads. Is there a particular reason why threads in a stopped pool are not destroyed? A couple of lines inserted into stop-pool would destroy the no-longer-used threads:

(defmethod stop-pool ((thread-pool thread-pool))
  (with-accessors ((threads threads)
           (pool-size pool-size)
           (running-p running-p)
           (pool-lock pool-lock)
           (pool-condition-vars pool-condition-vars)
           (main-thread main-thread))
      thread-pool
    (setf running-p nil)
    (bordeaux-threads:with-lock-held (pool-lock)
      (bordeaux-threads:destroy-thread main-thread)
      (map nil #'bordeaux-threads:destroy-thread threads)
      (setf main-thread nil
        threads nil
         pool-condition-vars nil))))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.