www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - shared associative array initialization

reply "japplegame" <japplegame gmail.com> writes:
I have no idea how to simple initialize shared associative array.
This code compiled but causing access violation when running.

shared uint[char] arr;

shared static this() {
   // next expression seems wrong, because
   // arr is process related and ['a':1, 'b':2] is thread related
   // probably, should be implicit cast error
   arr = ['a':1, 'b':2];
   // this is okay, but too cumbersome
   arr['c'] = 3;
   arr['d'] = 4;
}

void main() {
}

DMD32 D Compiler v2.059 on Windows 7 64 bit
May 13 2012
parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Sunday, 13 May 2012 at 16:25:42 UTC, japplegame wrote:
 I have no idea how to simple initialize shared associative 
 array. This code compiled but causing access violation when 
 running.

 shared uint[char] arr;

 shared static this() {
   // next expression seems wrong, because
   // arr is process related and ['a':1, 'b':2] is thread related
   // probably, should be implicit cast error
   arr = ['a':1, 'b':2];
   // this is okay, but too cumbersome
   arr['c'] = 3;
   arr['d'] = 4;
 }
I'm not sure, but I don't think the static this should be shared; only the data. static this should only runs once during program startup, otherwise you might be trying to do it per thread which defeats the shared purpose anyways. I've tried it but the problem remained. If your array is immutable you won't need shared at all and it should be available.
May 13 2012