Acess variable that was set by thread

Ali Çehreli acehreli at yahoo.com
Mon Aug 8 13:53:35 UTC 2022


On 8/8/22 00:14, vc wrote:

 > i will like to hear thoughts even if it works
 > for me

__gshared would work as well but I would consider std.concurrency first. 
Just a simple example:

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

struct Result {
   int value;
}

struct Done {
}

void run()
{
   bool done = false;
   while (!done) {
     writeln("Derived thread running.");
     receiveTimeout(1.seconds,
         (Done msg) {
             done = true;
         });
   }

   // Send the result to the owner
   // (I made assumptions; the thread may produce
   // results inside the while loop above.)
   ownerTid.send(Result(42));
}

void main()
{
     auto worker = spawn(&run);
     Thread.sleep(5.seconds);
     worker.send(Done());
     auto result = receiveOnly!Result();
     writeln("Here is the result: ", result);
}

Related, Roy Margalit's DConf 2022 presentation was based on traps 
related to sequential consistency. The video will be moved to a better 
place but the following link should work for now:

   https://youtu.be/04gJXpJ1i8M?t=5658

Ali



More information about the Digitalmars-d-learn mailing list