Search code examples
multithreadingd

user defined struct can't be passed through tid.send


I have created a mutlithreaded simulator that relies heavily on the native message passing between threads (don't go telling me to go single threaded it's for my thesis on D and I need to get this to work)

after a very durty kludge involving a lot of casts of objects to and from shared. which prolly had some ugly race condition bugs. I decided to create an opaque type that represents an object that can receive messages that should be able to be passed around without all that casting...

no such luck

struct OpaqueFaseSim{
    Tid tid;
    void send(...){...}
}

void foo(){
Tid tid;
long time;
    OpaqueFaseSim ofs;
    //...
    tid.send(ofs,time);//Error: static assert  "Aliases to mutable thread-local data not allowed."
}

why can I pass a Tid around but not a struct containing only a Tid?

and how can I fix this


Solution

  • I think it's because Tid has a MessageBox field which is a class type.

    You can type OpaqueFaseSim's tid field as shared or ___gshared and it will work:

    struct OpaqueFaseSim{
        Bar bar;
        shared Tid tid;
        // __gshared Tid tid;
    }