Jump to page: 1 2
Thread overview
Passing associative array to another thread
Sep 21, 2012
Martin Drasar
Sep 21, 2012
Jacob Carlborg
Sep 22, 2012
Martin Drasar
Sep 22, 2012
Jacob Carlborg
Sep 22, 2012
Jonathan M Davis
Sep 22, 2012
Martin Drasar
Sep 22, 2012
Johannes Pfau
Sep 22, 2012
Martin Drasar
Sep 23, 2012
Jacob Carlborg
Sep 24, 2012
Sean Kelly
Sep 25, 2012
Jacob Carlborg
Sep 25, 2012
Jacob Carlborg
Sep 25, 2012
Martin Drašar
September 21, 2012
Hi,

I am using the std.concurrency module and I would like to send an associative array to another thread.

If I try this:

string[string] aa;
someThread.send(aa);

I get: Aliases to mutable thread-local data not allowed.

And if I try to use this:

immutable(string[string]) aa;
someThread.send(aa);

I get:
/usr/include/d/dmd/phobos/std/variant.d(539): Error: *p is not mutable

which is because the send() creates a Message struct that stores the
data in a Variant.

And now I am stuck, because I do not have any idea what to do. Any advice?

Thanks,
Martin
September 21, 2012
On 2012-09-21 16:33, Martin Drasar wrote:
> Hi,
>
> I am using the std.concurrency module and I would like to send an
> associative array to another thread.
>
> If I try this:
>
> string[string] aa;
> someThread.send(aa);
>
> I get: Aliases to mutable thread-local data not allowed.
>
> And if I try to use this:
>
> immutable(string[string]) aa;
> someThread.send(aa);
>
> I get:
> /usr/include/d/dmd/phobos/std/variant.d(539): Error: *p is not mutable
>
> which is because the send() creates a Message struct that stores the
> data in a Variant.
>
> And now I am stuck, because I do not have any idea what to do. Any advice?

Perhaps declaring the associative array as "shared". An alternative would be to serialize the aa, pass it to another thread, and deserialize it. That would though create a copy.

-- 
/Jacob Carlborg
September 22, 2012
On 21.9.2012 19:01, Jacob Carlborg wrote:
> Perhaps declaring the associative array as "shared". An alternative would be to serialize the aa, pass it to another thread, and deserialize it. That would though create a copy.

Hi Jacob,

thanks for the hint. Making it shared sounds a bit fishy to me. My intention is to pass some read only data, that are in fact thread local and there is no real need to make them shared.

The (de)serialization is possible but the overhead seems a bit pointless. I will alter the code to use something else than AAs if there is no other way.

The data I am trying to pass is in fact just name-value pairs. I have tried to use Tuples, but I have hit another batch of problems. One was related to issue #5783, but another made me really scratch my head...

This compiles ok:
struct S { Tuple!int i; }

This does not:
struct S {
  Tuple!int i;
  SysTime   t;
}

Error: function
std.typecons.Tuple!(int).Tuple.opEquals!(const(Tuple!(int))).opEquals
(const(Tuple!(int)) rhs) is not callable using argument types
(const(Tuple!(int))) const

This looks a lot like the #5783, but I don't understand, why it only shows up with the SysTime in place...

Martin
September 22, 2012
On 2012-09-22 11:24, Martin Drasar wrote:

> thanks for the hint. Making it shared sounds a bit fishy to me. My
> intention is to pass some read only data, that are in fact thread local
> and there is no real need to make them shared.

The whole point of thread local data is that it's only accessible from a single thread. If you want to share it with another thread you have, as far as I know, there options:

1. Declare it as "shared"
2. Declare it as "immutable"
3. Make a copy, i.e. serialize the data

> The (de)serialization is possible but the overhead seems a bit
> pointless. I will alter the code to use something else than AAs if there
> is no other way.
>
> The data I am trying to pass is in fact just name-value pairs. I have
> tried to use Tuples, but I have hit another batch of problems. One was
> related to issue #5783, but another made me really scratch my head...

Looking at your original example I don't understand why the immutable aa won't work. That's the whole point of immutable, it's safe to share among threads. It's probably a bug somewhere. I think someone else can answer these questions better than me.

-- 
/Jacob Carlborg
September 22, 2012
On Saturday, September 22, 2012 12:30:30 Jacob Carlborg wrote:
> Looking at your original example I don't understand why the immutable aa won't work. That's the whole point of immutable, it's safe to share among threads. It's probably a bug somewhere. I think someone else can answer these questions better than me.

The problem with immutable is probably due to this bug:

http://d.puremagic.com/issues/show_bug.cgi?id=5538

And casting to shared probably won't work due to this bug:

http://d.puremagic.com/issues/show_bug.cgi?id=6585

std.variant needs quite a bit of work done to it, and it's causing problems with std.concurrency is this case. In the interim, I suspect that just about the only way to get an AA across threads is to just make it shared and not use std.concurrency at all, as undesirable as that may be. Your serialization suggestion would probably be the only other choice, though that would require something like Orange, as Phobos doesn't have such facilities.

- Jonathan M Davis
September 22, 2012
On 22.9.2012 13:19, Jonathan M Davis wrote:
> The problem with immutable is probably due to this bug:
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=5538
> 
> And casting to shared probably won't work due to this bug:
> 
> http://d.puremagic.com/issues/show_bug.cgi?id=6585
> 
> std.variant needs quite a bit of work done to it, and it's causing problems with std.concurrency is this case. In the interim, I suspect that just about the only way to get an AA across threads is to just make it shared and not use std.concurrency at all, as undesirable as that may be. Your serialization suggestion would probably be the only other choice, though that would require something like Orange, as Phobos doesn't have such facilities.
> 
> - Jonathan M Davis

Hi Jonathan,

I will work around the AA. As I have said, it is used only to pass name-value pairs. So no need to ditch the entire std.concurrency because of that.

Martin
September 22, 2012
Am Sat, 22 Sep 2012 12:30:30 +0200
schrieb Jacob Carlborg <doob@me.com>:

> On 2012-09-22 11:24, Martin Drasar wrote:
> 
> > thanks for the hint. Making it shared sounds a bit fishy to me. My intention is to pass some read only data, that are in fact thread local and there is no real need to make them shared.
> 
> The whole point of thread local data is that it's only accessible from a single thread. If you want to share it with another thread you have, as far as I know, there options:
> 
> 1. Declare it as "shared"
There's also __gshared.

> 2. Declare it as "immutable"
> 3. Make a copy, i.e. serialize the data

September 22, 2012
On 22.9.2012 13:50, Johannes Pfau wrote:
>> 1. Declare it as "shared"
> There's also __gshared.

Yup, that works.

Thanks
September 23, 2012
On 2012-09-22 13:50, Johannes Pfau wrote:

>> 1. Declare it as "shared"
> There's also __gshared.

Yeah, forgot about that one.

-- 
/Jacob Carlborg
September 24, 2012
On Sep 22, 2012, at 2:24 AM, Martin Drasar <drasar@ics.muni.cz> wrote:

> On 21.9.2012 19:01, Jacob Carlborg wrote:
>> Perhaps declaring the associative array as "shared". An alternative would be to serialize the aa, pass it to another thread, and deserialize it. That would though create a copy.
> 
> Hi Jacob,
> 
> thanks for the hint. Making it shared sounds a bit fishy to me. My intention is to pass some read only data, that are in fact thread local and there is no real need to make them shared.

If you're passing via std.concurrency then you'll currently have to cast to shared.  I'd been considering allowing Unique!T to be sent as well, but haven't done so yet.
« First   ‹ Prev
1 2