[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

sbcl sb-thread library

Jim Newton

7/3/2015 1:44:00 PM

Can someone give tell me how to start a list of threads in sbcl, and wait until they've finished?
For example:


(let ((running (mapcar (lambda (n)
(sb-thread:make-thread
(lambda ()
(format t "n = ~A~%" n)
(finish-output))))
'(1 2 3 4))))
(wait-until-finished running))

If I know in advance how many threads I'm going to start, I could set up some sort of counter, like the following. But there must be an easier way, especially in the case that I don't know before hand how many threads I wish to start.

Besides this implementation has the disadvantage that the "all finished" message is printed in the last thread to finish, not in the main thread where I want it.

(defun 12-threads ()
(let ((max-how-many 12)
(how-many 0)
(mutex (sb-thread:make-mutex)))

(flet ((finished? ()
(sb-thread:with-mutex (mutex)
(incf how-many)
(eql how-many max-how-many))))
(dotimes (n max-how-many)
(let ((n n))
(sb-thread:make-thread
(lambda ()
;; do some work
(format t "n = ~A~%" n)
(when (finished?)
(format t "all finished~%")))))))))
3 Answers

Jim Newton

7/3/2015 2:03:00 PM

0

I tried to do something like the following, but it fails with a Recursive lock attempt error.
The code tires to set MUTEX2 to locked, and have the last thread that finishes release the mutex.
and then with-mutex (mutex2) in the main thread. Unfortunately, it seems the main thread cannot wait for a mutex that it itself locked :-(

(defun 12-threads ()
(let ((max-how-many 12)
(how-many 0)
(mutex (sb-thread:make-mutex))
(mutex2 (sb-thread:make-mutex)))

(sb-thread:grab-mutex mutex2)
(flet ((finished? ()
(sb-thread:with-mutex (mutex)
(incf how-many)
(eql how-many max-how-many))))
(dotimes (n max-how-many)
(let ((n n))
(sb-thread:make-thread
(lambda ()
;; do some work
(format t "n = ~A~%" n)
(when (finished?)
(sb-thread:release-mutex mutex2))))))

(sb-thread:with-mutex (mutex2)
(format t "all finished~%")))))

Nicolas Neuss

7/3/2015 2:51:00 PM

0

Jim Newton <jimka.issy@gmail.com> writes:

> Can someone give tell me how to start a list of threads in sbcl, and
> wait until they've finished?

Use lparallel and something like:

(setf lparallel:*kernel* (lparallel:make-kernel N))
(lparallel:broadcast-task (lambda () "Hi"))

Nicolas

Zach Beane

7/4/2015 2:07:00 AM

0

Jim Newton <jimka.issy@gmail.com> writes:

> Can someone give tell me how to start a list of threads in sbcl, and wait until they've finished?
> For example:
>
>
> (let ((running (mapcar (lambda (n)
> (sb-thread:make-thread
> (lambda ()
> (format t "n = ~A~%" n)
> (finish-output))))
> '(1 2 3 4))))
> (wait-until-finished running))
>
> If I know in advance how many threads I'm going to start, I could set
> up some sort of counter, like the following. But there must be an
> easier way, especially in the case that I don't know before hand how
> many threads I wish to start.

I believe you could use (mapcar #'sb-thread:join-thread running). But
lparallel gives a nice abstraction, too.

Zach