<div>I used to think the "write" will block till the consumer thread "read" the same port under "buffering: #f". (which simulates golang's channel)</div><div><br></div><div>Now I can kind of understand that the "buffering" here refer to the "write" to port,rather than "write" to "consumers".</div>
<div><div><br><br><div class="gmail_quote">On Wed, Jan 25, 2012 at 11:15 AM, Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
On 2012-01-22, at 12:09 PM, Meng Zhang wrote:<br>
<br>
> I expect the reading option may block the thread with "buffering: #f".<br>
><br>
> For example:<br>
> (let ((chan (open-vector '(buffering: #f))))<br>
>   (thread-start!<br>
>     (make-thread<br>
>       (lambda ()<br>
>         (write "Value" chan)<br>
>         (pp "Write done"))))<br>
>   (thread-start!<br>
>     (make-thread<br>
>       (lambda ()<br>
>         (thread-sleep! 1)<br>
>         (pp "Start reading")<br>
>         (pp (read chan)))))<br>
>   (thread-sleep! 5))<br>
><br>
> My expectation:<br>
> Outputs:<br>
> "Start reading"<br>
> "Write done" or "Value", in any order.<br>
><br>
> Result for 4.6.3, osx 10.6.7:<br>
> Outputs:<br>
> "Write done"<br>
> "Start reading"<br>
> "Value"<br>
><br>
> Obviously, the "read" option doesn't block the spawned thread even "buffering" is set to #f.<br>
> Did I misunderstand it?<br>
><br>
<br>
</div></div>When you set buffering to #t, you are telling the runtime system that it is allowed to buffer the data that is output.  The runtime system may drain the buffers at anytime it wishes.  In other words setting buffering to #f guarantees that the data is not buffered, but there are no guarantees when buffering is #t.<br>

<br>
Here's another example derived from yours:<br>
<br>
(let ((chan (open-vector '(buffering: #t))))<br>
<br>
  (thread-start!<br>
    (make-thread<br>
      (lambda ()<br>
        (let loop ()<br>
          (thread-sleep! .2)<br>
          (write "Value" chan)<br>
          ;;(force-output chan)<br>
          (pp "Write done")<br>
          (loop)))))<br>
<br>
  (thread-start!<br>
    (make-thread<br>
      (lambda ()<br>
        (let loop ()<br>
          (pp "Start reading")<br>
          (pp (list 'got (read chan)))<br>
          (loop)))))<br>
<br>
  (thread-sleep! 5))<br>
<br>
As it is, the second thread will read from the vector port in bursts.  What is perhaps surprising is that the second thread starts waiting for data to read, then the first thread writes the first data, and the second thread immediately reads it.  That's because the first buffer is allocated lazily when the first data is written.  On the following writes by the first thread, the second thread will wait until the buffer is full.  To get synchronous writes and reads, either buffering should be set to #f, or the (force-output chan) should be uncommented.<br>

<span class="HOEnZb"><font color="#888888"><br>
Marc<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>Zhang Meng<br>School of Software Engineering, Tongji University<br>MP: +86 151-2103-2798<br>
</div></div>