Pascal
Pascal

Reputation: 443

Bordeaux-Threads: how to kill a thread?

I only found how to kill a thread that I have assigned to a variable:
(setf *foo* (bt:make-thread (lambda () (loop)) :name "Foo2")) --> (bt:destroy-thread *foo*)

How can I kill just any thread that I can see with (bt:all-threads):
(bt:make-thread (lambda () (loop)) :name "Foo") --> ?

Upvotes: 3

Views: 1233

Answers (2)

Rainer Joswig
Rainer Joswig

Reputation: 139261

You can kill any thread. There is nothing special about it. If you get a list of threads, just get the thread you want to kill and pass it to the function.

The function destroy-thread does not see a variable. Since it is a function, Lisp uses the usual evaluation rules. It gets passed a thread. The thread just happens to be the value of a variable in your example.

It could be the value of a function call:

(defun my-thread ()
  *foo*)

(bt:destroy-thread (my-thread))

or even part of a data structure, for example a list:

(defun my-thread ()
  (list 1 *foo* 3))

(bt:destroy-thread (second (my-thread)))

A thread is just another object.

If you get a list of threads, then you need to identify the correct thread. For example by looking at the name of the thread.

Upvotes: 8

PuercoPop
PuercoPop

Reputation: 6807

(bt:destroy-thread (nth index (bt:all-threads)))

It maybe be good the check if thread is alive, (bt:thread-alive-p <thread>) and not the current one, (bt:current-thread <thread>) Before killing it..

Upvotes: 7

Related Questions