Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A simple Common Lisp thread pool
Common Lisp
tree: 53af730807

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src
LICENSE
README
thread-pool.asd

README

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)
	     (flet ((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> (defun fib (n)
	   (if (> n 2)
	       (+ (fib (- n 1)) (fib (- n 2)))
	       1))
FIB
CL-USER> (defun make-lazy-fib (n)
	     (lambda ()
	       (format t "fib ~a = ~a~%" n (fib n))))
MAKE-LAZY-FIB
CL-USER> (progn (thread-pool:add-to-pool *pool* (make-lazy-fib 45))
		(thread-pool:add-to-pool *pool* (make-lazy-fib 20))
		(thread-pool:add-to-pool *pool* (make-lazy-fib 5)))

(#<CLOSURE (LAMBDA #) {1002DD2D49}>) 
(#<CLOSURE (LAMBDA #) {1002DD2D49}> #<CLOSURE (LAMBDA #) {1002DD4DB9}>) 
(#<CLOSURE (LAMBDA #) {1002DD4DB9}> #<CLOSURE (LAMBDA #) {1002DD68C9}>) 
1
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)))

(#<CLOSURE (LAMBDA #) {1002E7B009}>) 
(#<CLOSURE (LAMBDA #) {1002E7B009}> #<CLOSURE (LAMBDA #) {1002E7BF39}>) 
(#<CLOSURE (LAMBDA #) {1002E7B009}> #<CLOSURE (LAMBDA #) {1002E7BF39}>
 #<CLOSURE (LAMBDA #) {1002E7D4E9}>) 
1
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 #) {1002F87EB9}>) 
(#<CLOSURE (LAMBDA #) {1002F87EB9}> #<CLOSURE (LAMBDA #) {1002F90ED9}>) 
(#<CLOSURE (LAMBDA #) {1002F87EB9}> #<CLOSURE (LAMBDA #) {1002F90ED9}>
 #<CLOSURE (LAMBDA #) {1002F92489}>) 
(#<CLOSURE (LAMBDA #) {1002F92489}> #<CLOSURE (LAMBDA #) {1002F94159}>) 
(#<CLOSURE (LAMBDA #) {1002F92489}> #<CLOSURE (LAMBDA #) {1002F94159}>
 #<CLOSURE (LAMBDA #) {1002F95709}>) 
(#<CLOSURE (LAMBDA #) {1002F94159}> #<CLOSURE (LAMBDA #) {1002F95709}>
 #<CLOSURE (LAMBDA #) {1002F973D9}>) 
1
CL-USER> (let ((out *standard-output*))
	   (defun make-lazy-fib (n)
	     (lambda ()
	       (format out "fib ~a = ~a~%" n (fib n)))))
STYLE-WARNING: redefining COMMON-LISP-USER::MAKE-LAZY-FIB in DEFUN
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 #) {10057A4AB9}>) 
(#<CLOSURE (LAMBDA #) {10057A4AB9}> #<CLOSURE (LAMBDA #) {10057A59E9}>) 
(#<CLOSURE (LAMBDA #) {10057A4AB9}> #<CLOSURE (LAMBDA #) {10057A59E9}>
 #<CLOSURE (LAMBDA #) {10057A6F99}>) fib 15 = 610

(#<CLOSURE (LAMBDA #) {10057A6F99}> #<CLOSURE (LAMBDA #) {10057C0BB9}>) 
(#<CLOSURE (LAMBDA #) {10057A6F99}> #<CLOSURE (LAMBDA #) {10057C0BB9}>
 #<CLOSURE (LAMBDA #) {10057C2169}>) fib 20 = 6765
fib 5 = 5

(#<CLOSURE (LAMBDA #) {10057C2169}> #<CLOSURE (LAMBDA #) {10057C3E39}>) fib 17 = 
1fib 51970
 = 55
fib 6 = 8
1
CL-USER> (let ((out *standard-output*))
	   (defun make-lazy-fib (n)
	     (lambda ()
	       (format out "~%fib ~a = ~a~%" n (fib n)))))
STYLE-WARNING: redefining COMMON-LISP-USER::MAKE-LAZY-FIB in DEFUN
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
Something went wrong with that request. Please try again.