ddmd

d language thread


How to properly pass a handle using core.thread in D? I have tried to do it like this, but the handle will change and I don't know why:

void WorkerThread(handle hand) 
{
    …
}

…

auto worker = new Thread( { WorkerThread( m_handle ); } );

Solution

  • The Thread constructor can take a delegate that can have context. In the code shown, the context is the enclosing function. If that is a problem for some reason you should be able to do something like this:

    void StartThread(handle hand) {
      struct Con {
        handle m_handle;
        void Go() { WorkerThread( m_handle ); }
      }
    
      Con con = new Con;
      con.m_handle = hand;
      auto worker = new Thread( &con.Go );
    }