# prodcons.newlisp - benchmark Producer/consumer 
# newLISP v.8.3.0 or later required
# Note that newLISP does not use Pthreads but classic UNIX/fork() processes 
# therefore no mutexes amd condition variables but semaphores and shared memory

(constant 'wait -1 'sig 1)

(define (consumer n)
	(set 'i 0)
	(while (< i n)
		(semaphore cons-sem wait)
		(set 'i (share data))
		(share consumed (+ (share consumed) 1))
		(semaphore prod-sem sig))  
(define (producer n)
	(for (i 1 n)
		(semaphore prod-sem wait)
		(share data i)
		(share produced (+ (share produced) 1))
		(semaphore cons-sem sig))   

(define (main n)
	(set 'produced (share)) ; get shared mem addresses
	(set 'consumed (share))
	(set 'data (share))	

	(share produced 0) ; init shared memory
	(share consumed 0)
	(share data 0)

	(set 'prod-sem (semaphore)) ; get semaphores
	(set 'cons-sem (semaphore))

	(set 'prod-pid (fork (producer n))) ; start processes
	(set 'cons-pid (fork (consumer n)))
	(semaphore prod-sem sig) ; get producer started

	(wait-pid prod-pid) ; wait for processe to finish
	(wait-pid cons-pid)
	(semaphore cons-sem 0) ; release semaphore
	(semaphore prod-sem 0) ; release semaphore

	(println (share produced) " " (share consumed)))

(main (integer (last (main-args))))



syntax highlighting with newLISP and syntax.cgi