我正在使用并发lisp编程语言实现计算器程序。一切正常。但我想打印当前正在执行的线程和特定功能......无论我能够获得什么使用list-all-threads函数的线程列表...请建议我怎么做..
以下是我的输出::
的代码(defvar a) // defines a var but does not initialises it
(defvar b)
(defvar c)
(defvar d)
(write-line " Enter two numbers in binary format with prefix #b : ") // gives newline afterwords
(setf a (read))
/* SETF is a macro which uses SETQ internally, but has more possibilities. In a way it's a more general assignment operator.*/
(setf b(read))
(sb-thread:make-thread(lambda()(progn (sleep 4)(setf c(+ a b))
(print "ADDITION in binary: ")
(format t " ~b" c )
(print "ADDITION in decimal: ")
(print c)
)
)
)
(sb-thread:make-thread(lambda()(progn(sleep 2)(setf c(- a b))
(print "SUBTRACTION in binary: ")
(format t " ~b" c )
(print "SUBTRACTION in decimal: ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 5)(setf c(* a b))
(print "MULTIPLICATION in binary: ")
(format t " ~b" c )
(print "MULTIPLICATION IN DECIMAL: ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 3)(setf c(* a a))
(print "SQUARE in binary: ")
(format t " ~b" c )
(print "SQUARE OF 1st NUMBER : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 1)(setf c(* b b b))
(print "CUBE OF 2ND NUMBER : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 2)(setf c(sin a))
(print "SINE OF 1ST NUMBER : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 3)
(setf c(tan a))
(print "TAN OF 1ST NUMBER : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 6)
(setf c(cos a))
(print "COSINE OF 1ST NUMBER : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 2)
(setf c(min a b))
(print "MINIMUM NUMBER : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 1)
(setf c(max a b))
(print "MAXIMUM NUMBER : ")
(print c))))
(print (sb-thread:list-all-threads))
(sleep 30 )
(exit)
----------------------------------------------------------------------------------------
OUTPUT:
[CGL@localhost ~]$ sbcl
This is SBCL 1.2.8.14-8b3c9d0, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
* (load "test.lisp")
Enter two numbers in binary format with prefix #b :
#b1100
#b1011
(#<SB-THREAD:THREAD RUNNING {100322E8E3}>
#<SB-THREAD:THREAD RUNNING {10031E4263}>
#<SB-THREAD:THREAD RUNNING {1003190D33}>
#<SB-THREAD:THREAD RUNNING {10031750D3}>
#<SB-THREAD:THREAD RUNNING {1003157A73}>
#<SB-THREAD:THREAD RUNNING {10031393E3}>
#<SB-THREAD:THREAD RUNNING {1003119E43}>
#<SB-THREAD:THREAD RUNNING {10030D91C3}>
#<SB-THREAD:THREAD RUNNING {1003097C33}>
#<SB-THREAD:THREAD RUNNING {1003057903}>
#<SB-THREAD:THREAD "main thread" RUNNING {1002FCE543}>)
"CUBE OF 2ND NUMBER : "
1331
"MAXIMUM NUMBER : "
12
"SUBTRACTION in binary: " 1
"SUBTRACTION in decimal: "
1
"SINE OF 1ST NUMBER : "
-0.53657293
"MINIMUM NUMBER : "
11
"SQUARE in binary: " 10010000
"SQUARE OF 1st NUMBER : "
144
"TAN OF 1ST NUMBER : "
-0.6358599
"ADDITION in binary: " 10111
"ADDITION in decimal: "
23
"MULTIPLICATION in binary: " 10000100
"MULTIPLICATION IN DECIMAL: "
132
"COSINE OF 1ST NUMBER : "
0.84385395 [CGL@localhost ~]$
答案 0 :(得分:2)
Theads可以有名称,使用关键字名称来创建这样的线程
CL-USER> (defparameter empty-plus-thread (sb-thread:make-thread #'(lambda () (sleep 10 ) (+))
:name "what is the sum of no things adding?") )
EMPTY-PLUS-THREAD
CL-USER> (SB-THREAD:list-all-threads)
(#<SB-THREAD:THREAD "what is the sum of no things adding?" RUNNING
{10045998B3}>
#<SB-THREAD:THREAD "repl-thread" RUNNING {1005C880B3}>
#<SB-THREAD:THREAD "auto-flush-thread" RUNNING {1005C87DF3}>
#<SB-THREAD:THREAD "swank-indentation-cache-thread" RUNNING {10050701D3}>
#<SB-THREAD:THREAD "reader-thread" RUNNING {1005070003}>
#<SB-THREAD:THREAD "control-thread" RUNNING {100506F603}>
#<SB-THREAD:THREAD "Swank Sentinel" RUNNING {1004DE21E3}>
#<SB-THREAD:THREAD "main thread" RUNNING {1002C0F0A3}>)
CL-USER> (sb-thread:join-thread empty-plus-thread)
0
然后你也可以在线程中使用变量sb-thread:*current-thread*
在执行时获取或打印名称。
答案 1 :(得分:2)
在SBCL中,当前线程对象绑定到sb-thread:*current-thread*
特殊变量。
* sb-thread:*current-thread*
#<SB-THREAD:THREAD "main thread" RUNNING {1002AC3C43}>
要获取或设置线程对象的名称,请使用sb-thread:thread-name
:
* (sb-thread:thread-name sb-thread:*current-thread*)
"main thread"
也可以在构造期间通过传递&key
name
参数来设置线程的名称:
(sb-thread:make-thread
#'(lambda () (format t "Hello from thread ~A" sb-thread:*current-thread*))
:name "my-thread-0")
此外,您应该以某种方式同步打印调用(例如在全局变量中使用sb-thread:mutex
对象),否则输出可能会混乱。