concurrency problem with pointers

Ali Çehreli acehreli at yahoo.com
Fri Jul 19 08:36:33 PDT 2013


On 07/19/2013 08:16 AM, evilrat wrote:

 > here is reduced example without platform-specific stuff, requires
 > derelict3 and glfw.
 > http://pastebin.com/rhB14YNs

Further reduced by removing the need for derelict3 and glfw:

import std.stdio;
import std.concurrency;
import core.thread;

alias GLFWwindow = void;

struct Window {
     void create() {
         auto tid = spawn(&runNewView);
         send(tid, _wnd );        // <-- ERROR

         thread_joinAll();
     }

     GLFWwindow* _wnd;
}

void runNewView() {
     while ( true ) {
         receive (
             (string msg) { writeln(msg); },
             (const(GLFWwindow*) window) { }
                  );
     }
}

void main() {
     auto window = Window();
     // runs main loop
     window.create();
}

Error: static assert  "Aliases to mutable thread-local data not allowed."
        instantiated from here: send!(void*)

Is that the problem? (If so, why don't you say so? ;))

Then there are two solutions:

a) Make _wnd a shared(GLFWwindow):

struct Window {
     // ...

     shared(GLFWwindow)* _wnd;
}

The code now compiles.

b) As Sean Kelly said, cast to shared before sending:

         send(tid, cast(shared(GLFWwindow)*)_wnd );

With the b option you may need to change the definition of the receiving 
delegate to shared as well:

             (const(shared(GLFWwindow)*) window) { }

But that change is not needed in that simple code.

Ali

P.S. Additionally, the const on the receiving side probably should apply 
only to what is being pointed at (as opposed to the pointer itself):

             (const(GLFWwindow)* window) { }



More information about the Digitalmars-d-learn mailing list