digitalmars.D.learn - Passing associative array to another thread
- Martin Drasar (17/17) Sep 21 2012 Hi,
- Jacob Carlborg (6/21) Sep 21 2012 Perhaps declaring the associative array as "shared". An alternative
- Martin Drasar (25/28) Sep 22 2012 Hi Jacob,
- Jacob Carlborg (13/22) Sep 22 2012 The whole point of thread local data is that it's only accessible from a...
- Jonathan M Davis (12/16) Sep 22 2012 The problem with immutable is probably due to this bug:
- Martin Drasar (6/22) Sep 22 2012 Hi Jonathan,
- Johannes Pfau (3/16) Sep 22 2012 There's also __gshared.
- Martin Drasar (3/5) Sep 22 2012 Yup, that works.
- Jacob Carlborg (4/6) Sep 23 2012 Yeah, forgot about that one.
- Sean Kelly (6/16) Sep 24 2012 local
- Jacob Carlborg (7/8) Sep 24 2012 Hey, if it's immutable why use std.concurrency at all? Just import the
- Jacob Carlborg (6/18) Sep 25 2012 BTW, why do you need to use std.currency at all if it's immutable, just
- =?windows-1252?Q?Martin_Dra=9Aar?= (10/13) Sep 25 2012 It is not some single piece of data.
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
Sep 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
Sep 21 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 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 shows up with the SysTime in place... Martin
Sep 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 dataThe (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 wasLooking 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
Sep 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
Sep 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 DavisHi 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
Sep 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:There's also __gshared.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
Sep 22 2012
On 22.9.2012 13:50, Johannes Pfau wrote:Yup, that works. Thanks1. Declare it as "shared"There's also __gshared.
Sep 22 2012
On 2012-09-22 13:50, Johannes Pfau wrote:Yeah, forgot about that one. -- /Jacob Carlborg1. Declare it as "shared"There's also __gshared.
Sep 23 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:deserializePerhaps declaring the associative array as "shared". An alternative would be to serialize the aa, pass it to another thread, and =localit. That would though create a copy.=20 Hi Jacob, =20 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 =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.=
Sep 24 2012
On 2012-09-25 00:47, Sean Kelly wrote: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.Hey, if it's immutable why use std.concurrency at all? Just import the module and use the variable willy nilly. I mean, isn't that the whole point of immutable anyway, you can share it freely among threads without any risks? -- /Jacob Carlborg
Sep 24 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 mutableBTW, why do you need to use std.currency at all if it's immutable, just share it as a global. The whole point of immutable is that it can be freely shared among threads without any risks. -- /Jacob Carlborg
Sep 25 2012
Dne 25.9.2012 18:19, Jacob Carlborg napsal(a):BTW, why do you need to use std.currency at all if it's immutable, just share it as a global. The whole point of immutable is that it can be freely shared among threads without any risks.It is not some single piece of data. I have a queue of tasks. Each task is a struct that contains among other things a user-supplied delegate and an AA with parameters for that delegate. These tasks are created somewhere, inserted into the queue and then executed in parallel. Once created they are not modified anywhere and should exist only in one instance that is passed to that delegate. It's just that I haven't found a better way to pass parameters that are unknown before to a delegate. Martin
Sep 25 2012