manpagez: man pages & more
info bigloo
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

17.4 Mixing Thread APIs

The Threads library is “Posix Threads” safe, which means it is possible to use at the same time both libraries. In other words, it is possible to embed one fair scheduler into a Posix thread.

Here is a little example with two schedulers started into two different Posix threads, each schedulers running two fair threads.

(module mix_threads
   (library fthread pthread)
   (main main))

(define *f1* 0)
(define *f2* 0)

(define (main args)
   (let ((s1 (make-scheduler #t))
	 (s2 (make-scheduler #t))
	 
	 (f1a (instantiate::fthread
		 (body (lambda ()
			  (let loop ()
			     (print "f1a: " *f1* " " (current-thread))
			     (set! *f1* (+ 1 *f1*))
			     (thread-yield!)
			     (loop))))))
	 
	 (f1b (instantiate::fthread
		 (body (lambda ()
			  (let loop ()
			     (print "f1b: " *f1* " " (current-thread))
			     (set! *f1* (+ 1 *f1*))
			     (thread-yield!)
			     (loop))))))
	 
	 (f2a (instantiate::fthread
		 (body (lambda ()
			  (let loop ()
			     (print "f2a: " *f2* " " (current-thread))
			     (set! *f2* (+ 1 *f2*))
			     (thread-yield!)
			     (loop))))))
	 
	 (f2b (instantiate::fthread
		 (body (lambda ()
			  (let loop ()
			     (print "f2b: " *f2* " " (current-thread))
			     (set! *f2* (+ 1 *f2*))
			     (thread-yield!)
			     (loop)))))))
      
      (let* ((p1 (instantiate::pthread
		   (body (lambda ()
			    ;; Sets the thread's specific scheduler
			    (default-scheduler s1)
			    (scheduler-start! 5)))))
	     
	    (p2 (instantiate::pthread
		   (body (lambda ()
			    ;; Sets the thread's specific scheduler
			    (default-scheduler s2)
			    ;; One reaction for s2
			    (scheduler-react!)
			    ;; Starts s1
			    (thread-start-joinable! p1)
			    ;; Do three reactions
			    (scheduler-start! 3)
			    ;; Waits for p1/s1 termination
			    (thread-join! p1)
			    ;; The final reaction
			    (scheduler-react!))))))
	 
	 (thread-start! f1a s1)
	 (thread-start! f1b s1)
	 (thread-start! f2a s2)
	 (thread-start! f2b s2)
	 
	 (thread-join! (thread-start-joinable! p2)))))

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on October 23, 2011 using texi2html 5.0.