Thread overview
Immutable woes
Sep 21, 2010
Bob Cowdery
Sep 21, 2010
Simen kjaeraas
Sep 21, 2010
Bob Cowdery
Sep 21, 2010
Simen kjaeraas
Sep 21, 2010
Pelle
Sep 21, 2010
Bob Cowdery
Sep 21, 2010
Simen kjaeraas
September 21, 2010
 Hi

I'm stuggling with immutable.

I have a fixed size buffer which is used as a circular buffer of floats and is effectively double buffering data I wish to transfer to another thread. At an appropriate point I take the top half or bottom half of the buffer and send it to another thread.

To do this I need to copy the data to an immutable transfer buffer to use in the send. I'm sure this is simple but I can't figure it.

if I say something like:
float[] xfer = new float[512];
xfer = buffer[0 .. $/2];
tid.send(xfer);

it rightly tells me 'thread local data not allowed'. If I make it:

immutable (float)[] xfer;
xfer = buffer[0 .. $/2];
tid.send(xfer);

it tells me 'can't implicitly convert float[] to immutable (float)[]'

If I try a float by float copy into xfer it can't because I've said the buffer is immutable. In the first example I can't figure out how to convert the slice into an immutable copy which I think is what I should be doing.

Can someone point me in the right direction.

Thanks
Bob

September 21, 2010
Bob Cowdery <bob@bobcowdery.plus.com> wrote:

> if I say something like:
> float[] xfer = new float[512];
> xfer = buffer[0 .. $/2];
> tid.send(xfer);
>
> it rightly tells me 'thread local data not allowed'. If I make it:
>
> immutable (float)[] xfer;
> xfer = buffer[0 .. $/2];
> tid.send(xfer);
>
> it tells me 'can't implicitly convert float[] to immutable (float)[]'
>
> If I try a float by float copy into xfer it can't because I've said the
> buffer is immutable. In the first example I can't figure out how to
> convert the slice into an immutable copy which I think is what I should
> be doing.
>
> Can someone point me in the right direction.

http://www.digitalmars.com/d/2.0/phobos/std_exception.html#assumeUnique

float[] xfer = new float[512];
xfer = buffer[0 .. $/2];
tid.send(assumeUnique(xfer));

This should solve your problems.

-- 
Simen
September 21, 2010
 Thanks, that at least builds now. I didn't see that trick in the book
but I've found it on the on-line library ref under std.exception. I
can't say I understand why its under std.exception.

bob

On 21/09/2010 08:48, Simen kjaeraas wrote:
> Bob Cowdery <bob@bobcowdery.plus.com> wrote:
>
>> if I say something like:
>> float[] xfer = new float[512];
>> xfer = buffer[0 .. $/2];
>> tid.send(xfer);
>>
>> it rightly tells me 'thread local data not allowed'. If I make it:
>>
>> immutable (float)[] xfer;
>> xfer = buffer[0 .. $/2];
>> tid.send(xfer);
>>
>> it tells me 'can't implicitly convert float[] to immutable (float)[]'
>>
>> If I try a float by float copy into xfer it can't because I've said the buffer is immutable. In the first example I can't figure out how to convert the slice into an immutable copy which I think is what I should be doing.
>>
>> Can someone point me in the right direction.
>
> http://www.digitalmars.com/d/2.0/phobos/std_exception.html#assumeUnique
>
> float[] xfer = new float[512];
> xfer = buffer[0 .. $/2];
> tid.send(assumeUnique(xfer));
>
> This should solve your problems.
>

September 21, 2010
Bob Cowdery <bob@bobcowdery.plus.com> wrote:

>  Thanks, that at least builds now. I didn't see that trick in the book
> but I've found it on the on-line library ref under std.exception. I
> can't say I understand why its under std.exception.

I know. It used to be std.contracts, which makes a bit more sense. Then,
someone decided std.contracts should be all about exceptions, and
apparently forgot this exception.

-- 
Simen
September 21, 2010
On 09/21/2010 09:29 AM, Bob Cowdery wrote:
>   Hi
>
> I'm stuggling with immutable.
>
> I have a fixed size buffer which is used as a circular buffer of floats
> and is effectively double buffering data I wish to transfer to another
> thread. At an appropriate point I take the top half or bottom half of
> the buffer and send it to another thread.
>
> To do this I need to copy the data to an immutable transfer buffer to
> use in the send. I'm sure this is simple but I can't figure it.
>
> if I say something like:
> float[] xfer = new float[512];
> xfer = buffer[0 .. $/2];
> tid.send(xfer);
>
> it rightly tells me 'thread local data not allowed'. If I make it:
>
> immutable (float)[] xfer;
> xfer = buffer[0 .. $/2];
> tid.send(xfer);
>
> it tells me 'can't implicitly convert float[] to immutable (float)[]'
>
> If I try a float by float copy into xfer it can't because I've said the
> buffer is immutable. In the first example I can't figure out how to
> convert the slice into an immutable copy which I think is what I should
> be doing.
>
> Can someone point me in the right direction.
>
> Thanks
> Bob
>

immutable(float)[] xfer = buffer[0 .. $/2].idup;
tid.send(xfer);

Don't use assumeUnique for non-unique references.
September 21, 2010
 Ok, so assumeUnique when I read it a bit more will still share data but
idup will copy data. Where are the docs for that. Something else not in
the book - but then I guess there is a lot of detail not in the book.
Thank goodness for forums.

bob

On 21/09/2010 09:10, Pelle wrote:
> On 09/21/2010 09:29 AM, Bob Cowdery wrote:
>>   Hi
>>
>> I'm stuggling with immutable.
>>
>> I have a fixed size buffer which is used as a circular buffer of floats and is effectively double buffering data I wish to transfer to another thread. At an appropriate point I take the top half or bottom half of the buffer and send it to another thread.
>>
>> To do this I need to copy the data to an immutable transfer buffer to use in the send. I'm sure this is simple but I can't figure it.
>>
>> if I say something like:
>> float[] xfer = new float[512];
>> xfer = buffer[0 .. $/2];
>> tid.send(xfer);
>>
>> it rightly tells me 'thread local data not allowed'. If I make it:
>>
>> immutable (float)[] xfer;
>> xfer = buffer[0 .. $/2];
>> tid.send(xfer);
>>
>> it tells me 'can't implicitly convert float[] to immutable (float)[]'
>>
>> If I try a float by float copy into xfer it can't because I've said the buffer is immutable. In the first example I can't figure out how to convert the slice into an immutable copy which I think is what I should be doing.
>>
>> Can someone point me in the right direction.
>>
>> Thanks
>> Bob
>>
>
> immutable(float)[] xfer = buffer[0 .. $/2].idup;
> tid.send(xfer);
>
> Don't use assumeUnique for non-unique references.

September 21, 2010
Bob Cowdery <bob@bobcowdery.plus.com> wrote:

>  Ok, so assumeUnique when I read it a bit more will still share data but
> idup will copy data. Where are the docs for that. Something else not in
> the book - but then I guess there is a lot of detail not in the book.
> Thank goodness for forums.

The thing is, slices (as you did in xfer = buffer[0 .. $/2];) do not
copy data. I somehow misread your code as xfer[] = buffer[0 .. $/2];
which would copy data, if only xfer already had a length - sorry about that.

-- 
Simen