08.02.2015 Views

Programação Funcional e Concorrente com Scheme

Programação Funcional e Concorrente com Scheme

Programação Funcional e Concorrente com Scheme

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

notas de aula - Jerônimo C. Pellegrini<br />

third sent!<br />

A thread B chegou a mostrar a string "---> ", mas ficou bloqueada antes de mostrar<br />

a mensagem, e só voltou a executar depois que a thread A fez o último envio.<br />

12.1.1 Exemplo: produtor/consumidor<br />

O problema do produtor/consumidor é resolvido trivialmente usando uma mailbox.<br />

( let (( mbox ( make-mailbox )))<br />

( define produce<br />

( lambda ()<br />

( let loop ((i 0))<br />

( mailbox-send ! mbox ( list " data " i))<br />

( loop (+ i 1)))))<br />

( define consume<br />

( lambda ()<br />

( let ((x ( mailbox-receive ! mbox )))<br />

( display " received ")<br />

( display ( list-ref x 1))<br />

( newline ))<br />

( consume )))<br />

( let ((p ( make-thread produce ))<br />

(c ( make-thread consume )))<br />

( thread-start ! c)<br />

( thread-start ! p)<br />

( thread-join ! c)<br />

( thread-join ! p )))<br />

Versão Preliminar<br />

[ 27 de outubro de 2010 at 15:47 ]<br />

273

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!