Hi all,
Greeting from me! I am a newbie of newLISP, and want to implement a simple feature which launches processes according to CPU number:
(import "libc.so" "sysconf")
(constant '_SC_NPROCESSORS_ONLN 503)
(define (report pid)
(println "process: " pid " has returned"))
(define cpu-num (sysconf _SC_NPROCESSORS_ONLN))
(set 'result-array cpu-num)
(dotimes (i cpu-num)
(spawn 'p (println i)))
(until (true? (sync 10 report)))
(exit)
I know following code will let p gets only one result:
(dotimes (i cpu-num)
(spawn 'p (println i)))
I want to use an array to hold every process's return value, but following code is not invalid:
(set 'result-array cpu-num)
(dotimes (i cpu-num)
(spawn (result-array i) (println i)))
So how can I get return value of every spawn process iteration in for-loop? Thanks very much in advance!
Best Regards
Nan Xiao
You could do something like this
(dotimes (i cpu-num)
(spawn (last (push (sym (string "ret" i)) returns -1)) (println i)))
That'd make returns be a list of the return symbols, letting you do
(eval (returns k))
to obtain the return value of the k:th sub process.
Or, you may want an association list, as with the following
(dotimes (i cpu-num)
(let ((p (sym (string "ret" i))))
(push (list (spawn p (println i)) p) returns -1)))
Then you'd get the return value via
(eval (lookup pid returns))
It goes without saying, that the return values won't be set until the sub processes have finished.
@ralph.ronnquist:
Thanks very much for your help!