f



SBCL Threads: how to wait for a thread to exit?

Hi,

I need some help understanding how to wait for a SBCL thread to be
done (i.e. do a thread_join operation).

Starting the thread is easy, e.g.

(sb-thread:make-thread (lambda () (write-line "Hello, world")))

I can then look at all threads with sb-thread:list-all-threads and see
if specific thread is running with sb-thread:thread-alive-p.

However, I am unable to see an obvious way to do a blocking wait for a
thread to finish....many google searches got me nowhere.

So I looked in the SBCL source and, as far as my little mind can go,
it seems the thread code actually does not provide a way for me to do
this (in src/code/target-thread.lisp all handle-thread-exit does is
remove the thread from *all-threads* so it can be gc'ed when
appropriate; no hooks to watch for it that I could see, and the
underlying pthread join seems unexposed to the lisp programmer).

So...what am I missing? Or do I really need to roll my own
sychronization macros to wrap each thread start/stop with some
condition variables?

Cheers!
Shyamal

PS: I don't read the SBCL mailing list, so I thought I'd try here
first.


0
7/16/2006 6:45:02 AM
comp.lang.lisp 16861 articles. 5 followers. Post Follow

2 Replies
533 Views

Similar Articles

[PageSpeed] 16

Shyamal,

You might either want to use a lock and state variable or for more
flexibility and efficiency look at the waitqueue/condition variable stuff -
see

http://www.sbcl.org/manual/Waitqueue_002fcondition-variables.html#Waitqueue_002fcondition-variables

These provides allow you to have one or more threads wait using
condition-wait until another thread notifies them using
condition-notify. For example I am using this to have a thread which
carries out tasks in the background. The tasks are put on a queue by
other threads, which then notify the processing thread. These threads
may then return immediately, or if they need the result they can wait
until notified by the processing thread.

>>>>> "Shyamal" == Shyamal Prasad <shyamalprasad@verizon.net> writes:

    Shyamal> Hi,

    Shyamal> I need some help understanding how to wait for a SBCL
    Shyamal> thread to be done (i.e. do a thread_join operation).

    Shyamal> Starting the thread is easy, e.g.

    Shyamal> (sb-thread:make-thread (lambda () (write-line "Hello,
    Shyamal> world")))

    Shyamal> I can then look at all threads with
    Shyamal> sb-thread:list-all-threads and see if specific thread is
    Shyamal> running with sb-thread:thread-alive-p.

    Shyamal> However, I am unable to see an obvious way to do a
    Shyamal> blocking wait for a thread to finish....many google
    Shyamal> searches got me nowhere.

    Shyamal> So I looked in the SBCL source and, as far as my little
    Shyamal> mind can go, it seems the thread code actually does not
    Shyamal> provide a way for me to do this (in
    Shyamal> src/code/target-thread.lisp all handle-thread-exit does
    Shyamal> is remove the thread from *all-threads* so it can be
    Shyamal> gc'ed when appropriate; no hooks to watch for it that I
    Shyamal> could see, and the underlying pthread join seems
    Shyamal> unexposed to the lisp programmer).

    Shyamal> So...what am I missing? Or do I really need to roll my
    Shyamal> own sychronization macros to wrap each thread start/stop
    Shyamal> with some condition variables?

    Shyamal> Cheers!  Shyamal

    Shyamal> PS: I don't read the SBCL mailing list, so I thought I'd
    Shyamal> try here first.



-- 
John A.R. Williams 
PGP key: 6606795A185C384C
0
7/16/2006 7:44:57 AM
>>>>> "John" == John A R Williams <J.A.R.Williams@aston.ac.uk> writes:

    John> Shyamal,

    John> You might either want to use a lock and state variable or
    John> for more flexibility and efficiency look at the
    John> waitqueue/condition variable stuff - see

    Shyamal> [....]
    Shyamal> However, I am unable to see an obvious way to do a
    Shyamal> blocking wait for a thread to finish....many google
    Shyamal> searches got me nowhere.
    Shyamal> [....]
    Shyamal> could see, and the underlying pthread join seems
    Shyamal> unexposed to the lisp programmer).

Hi John,

Thanks for the response. I actually do use waitqueues in my code.  I
was hoping to find was a "primitive" thread join operation that waits
for a thread to terminate. Your response strengthens my suspicion that
SBCL does not provide this.

Right now I'm actually doing exactly what you suggest :)

Best regards,
Shyamal
0
7/16/2006 4:13:28 PM
Reply: