Re: On the implications of adding file-open to SRFI-170 Marc Feeley 28 Nov 2019 15:15 UTC

> On Nov 28, 2019, at 9:56 AM, Lassi Kortela <xxxxxx@lassi.io> wrote:
>
> Hmm. How would parts of strings get interleaved -- are there multi-threaded Scheme implementations that don't protect their port objects with mutexes?
>
> If the output port is buffered but also mutexed internally, isn't
>
> (write a)  ; in thread 1
> (write b)  ; in thread 2
> (write c)  ; in thread 3
>
> guaranteed to write a, b and c (in some order) so that none of their contents are intermingled?
>
> If there's no locking then the strings can indeed get mixed up, but wouldn't that be better fixed by adding a mutex instead of turning off buffering?

In Gambit, the implementation of “write” (and related procedures) causes characters to be written to the port, and the locking is done at the character level.  So indeed separate calls to write in 2 threads may end up interleaving the characters.  This is not a bug… for example you may not want individual calls to write to appear atomic if you are debugging the write procedure itself (and the debugging output goes to the same stdout as is being written to by “write”), which may be the case if the programmer has a way to extend the write procedure for specific types and the code in these extensions is buggy.  This exists in many Schemes, including Gambit.

Marc