Wolfgang Meyer wrote:
>
> Boriss Mejias wrote:
>> Raphael Collet wrote:
>>> Dear Ashis,
>>>
>>> The port only keeps a reference to the unbound tail of the stream.
>>> It does not keep alive all former messages that were sent.
>>
>> Sorry for joining late, but, if you have a pointer to the beginning
>> of the stream, then all the messages sent to that port will stay in
>> the stream and not be garbage collected. is that right? for instance:
>>
>> declare
>> S P
>> P = {NewPort S}
>> thread
>> for Msg in S do
>> {Browse Msg}
>> end
>> end
>>
>> That will keep the whole stream of messages because you can always
>> access S.
>>
>> An alternative code would be
>>
>> declare
>> P
>> thread
>> S
>> in
>> P = {NewPort S}
>> for Msg in S do
>> {Browse Msg}
>> end
>> end
>>
>> on this second code you don't need to keep the whole stream, and then
>> the garbage collector should be able to do its job.
>>
> I also like this short version of the code which I found in the Mozart
> sources a while ago:
>
> declare
> P
> thread
> for Msg in {NewPort $ P} do
> {Browse Msg}
> end
> end
>
> With this code, there is no temptation to make the scope of the stream
> too wide.
indeed, you protect the stream, but you do not protect P. the issue
with the two latter above is that P is bound (to a port) in a thread
different from that in which it is declared. since you have no
guarantees on when a thread is run, this solution has a hole: P can be
bound in another thread (e.g., the parent one) to whatever value,
causing an error when the child thread attempts to execute its NewPort.
this may not be a serious risk in some cases, but if, for example, you
want to distribute P and thus have no control over what is being done to
it there, there may be ugly surprises.
one quick solution to this would be to declare and bind P as well as
create the thread in one pass, so to speak:
declare
P = local P in
thread
for Msg in {NewPort $ P} do
{Browse Msg} end end
P end
the parent thread can only proceed while the newly declared P is bound
to the port created inside the local statement, and P cannot be bound to
whatever else in any other thread.
vQ
_________________________________________________________________________________
mozart-users mailing list
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users