Common lisp 获取SBCL中的线程ID

Common lisp 获取SBCL中的线程ID,common-lisp,sbcl,Common Lisp,Sbcl,我正在使用Lisp sb线程包。当我使用*current thread*获取线程id时,上一次计算表达式的结果也会与线程id一起返回。我只需要程序的线程id。SBCL具有setf可设置的名称,而不是id: 你需要ID做什么?生活中有些情况下你非常需要线程的ID,例如: “我遇到过这样一种情况:在Linux上的SBCL上,我有多个工人 我机器上的线程,其中一个占用了我100%的CPU 想要检索有问题线程的线程对象,但是 结果证明这不是小事。” 在手册页中,syscall函数的原型是long sy

我正在使用Lisp sb线程包。当我使用
*current thread*
获取线程id时,上一次计算表达式的结果也会与线程id一起返回。我只需要程序的线程id。

SBCL具有
setf
可设置的名称,而不是id:


你需要ID做什么?

生活中有些情况下你非常需要线程的ID,例如:

“我遇到过这样一种情况:在Linux上的SBCL上,我有多个工人 我机器上的线程,其中一个占用了我100%的CPU 想要检索有问题线程的线程对象,但是 结果证明这不是小事。”

在手册页中,syscall函数的原型是
long syscall(long number,…)
,因此参数和ret值的正确类型都是long,但我不确定如何使用sb-alien:types指定它

我查看了sbcl的src:

find . -name "*alien*" -exec echo {} \; -exec grep define-alien-type-tr {} \;
搜索结果: 要查看线程,可以使用以下命令:

ps -To pid,tid -p `pidof sbcl` 
);如果需要PID,请执行
(sb posix:getpid)
或调用“getpid”外星人:

如果您使用的是Windows,则可以使用以下代码(取自此处:) 更新结果表明,上面的windows代码也可以简化为一行代码:
sb-thread:*当前线程*
是线程对象本身,而不是ID…这是我拥有的赋值。我需要说明不同的函数在不同的线程上执行。在这种情况下,线程对象或其名称的打印表示就足够了。
find . -name "*alien*" -exec echo {} \; -exec grep define-alien-type-tr {} \;
./host-alieneval.lisp
  (defun %define-alien-type-translator (name translator)
(define-alien-type-translator system-area-pointer ()
(define-alien-type-translator signed (&optional (bits sb!vm:n-word-bits))
(define-alien-type-translator integer (&optional (bits sb!vm:n-word-bits))
(define-alien-type-translator unsigned (&optional (bits sb!vm:n-word-bits))
(define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits))
(define-alien-type-translator enum (&whole
(define-alien-type-translator single-float ()
(define-alien-type-translator double-float ()
(define-alien-type-translator * (to &environment env)
(define-alien-type-translator array (ele-type &rest dims &environment env)
(define-alien-type-translator struct (name &rest fields &environment env)
(define-alien-type-translator union (name &rest fields &environment env)
(define-alien-type-translator function (result-type &rest arg-types
(define-alien-type-translator values (&rest values &environment env)
# not sure which of them is the type for LONG
ps -To pid,tid -p `pidof sbcl` 
(sb-alien:alien-funcall 
   (sb-alien:extern-alien "getpid" 
       (function sb-alien:unsigned)) )
#|
typedef struct pthread_thread {
  pthread_fn start_routine;
  void* arg;
  HANDLE handle;
...
}
|#

(defun get-thread-handle (thread)
  "Retrieves WIN32 thread HANDLE from SBCL thread"
  (declare (type sb-thread:thread thread))
  (let* ((pthread-pointer
           (sb-sys:int-sap (sb-thread::thread-os-thread thread)))
         (pthread-alien
           (sb-alien:sap-alien
            pthread-pointer (sb-alien:struct nil
                                             (start-addr (* t))
                                             (arg (* t))
                                             (handle (* t))))))
    (sb-alien:alien-sap (sb-alien:slot pthread-alien 'handle))))

(defun get-thread-id (thread)
  "Retrieves WIN32 thread ID from SBCL thread"
  (declare (type sb-thread:thread thread))
  (sb-alien:alien-funcall
   (sb-alien:extern-alien "GetThreadId" (function sb-alien:unsigned
                                                  (* t)))
   (get-thread-handle thread)))

(get-thread-id sb-thread:*current-thread*) ; ==> 62
(sb-alien:alien-funcall (sb-alien:extern-alien "GetCurrentThreadId" (function sb-alien:unsigned)))