D threading and shared variables

Archie Allison archie_allison at compuserve.com
Sun Apr 7 20:35:15 UTC 2019


On Sunday, 7 April 2019 at 17:52:40 UTC, Mike Wey wrote:
>> 
>
> How are you using the GUI, GTK is not thread safe, all gui 
> function calls should be made from the GUI thread.
>
> Last time i checked threadsEnter and threadsLeave didn't work 
> properly on windows.

All GUI updates are sent from a worker thread to the GUI thread 
using the D message passing system and immutable object 
arguments. I'm pretty satisfied it's not a problem.

The general principle of the classes doing the comms are:

class name :Thread
{
   Mutex m;
   Condition sig;
   shared char[] shared_buffer;
   CommunicationClass comm;

   this()
   {
     m = new Mutex;
     sig = new Condition(m);
     comm = new CommunicationClass;
     super(&receive_thread);
     start();
   }

   //the main routine being called
   void packet_transaction()
   {
     comm.send_data();
     synchronized(m)
     {
       if (sig.wait(dur!("seconds")(1)) == true)
         do_something();
       else
         do_timeout();
     }
   }

   void receive_thread()
   {
     local_buffer;
     while(true)
     {
       local_buffer += comm.get_data();
       if (some condition)
       {
         synchronized(m)
         {
           copy local_buffer to shared_buffer;
           sig.notify();
         }
       }
     }
   }
};


More information about the Digitalmars-d-learn mailing list