multithreading - Implementing the calculator program using lisp -
i'm implementing calculator program using concurrent lisp programming language..everything working fine..but want print thread executing , particular function..however i'm able list of thread using list-all-threads function...please suggest me how it..
following code output::
(defvar a) // defines var not initialises (defvar b) (defvar c) (defvar d) (write-line " enter 2 numbers in binary format prefix #b : ") // gives newline afterwords (setf (read)) /* setf macro uses setq internally, has more possibilities. in way it's more general assignment operator.*/ (setf b(read)) (sb-thread:make-thread(lambda()(progn (sleep 4)(setf c(+ 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(- 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(* 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)) (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 b)) (print "minimum number : ") (print c)))) (sb-thread:make-thread(lambda()(progn(sleep 1) (setf c(max b)) (print "maximum number : ") (print c)))) (print (sb-thread:list-all-threads)) (sleep 30 ) (exit) ---------------------------------------------------------------------------------------- output: [cgl@localhost ~]$ sbcl sbcl 1.2.8.14-8b3c9d0, implementation of ansi common lisp. more information sbcl available @ <http://www.sbcl.org/>. sbcl free software, provided is, absolutely no warranty. in public domain; portions provided under bsd-style licenses. see credits , copying files in distribution more information. * (load "test.lisp") enter 2 numbers in binary format 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 ~]$
in sbcl current thread object bound sb-thread:*current-thread*
special variable.
* sb-thread:*current-thread* #<sb-thread:thread "main thread" running {1002ac3c43}>
to or set name of thread object, use sb-thread:thread-name
:
* (sb-thread:thread-name sb-thread:*current-thread*) "main thread"
the name of thread set during construction passing &key
name
argument:
(sb-thread:make-thread #'(lambda () (format t "hello thread ~a" sb-thread:*current-thread*)) :name "my-thread-0")
also should somehow synchronize printing calls (for example using sb-thread:mutex
object in global variable), because otherwise output messed up.
Comments
Post a Comment