A scheduled control signal with fibers?

Ali Çehreli acehreli at yahoo.com
Sun Sep 27 10:40:25 UTC 2020

On 9/27/20 3:06 AM, Ferhat Kurtulmuş wrote:

 > __gshared DList!Entry queue;
 > __gshared bool shouldRun = true;

Have you considered passing messages with std.concurrency.send() and 
std.concurrency.receive() and friends? You wouldn't need 'queue' because 
all of your threads already have mail boxes to send messages to each other.

 > void worker() {
 >      while(shouldRun){
 >          auto r = queue[];
 >          if(!r.empty && queue.back.st < Clock.currTime){
 >              writeln(queue.back); // consume the value
 > sendPWMSignalToValfe(pwmval)
 >              queue.popLastOf(r);
 >          }
 >      }
 > }

It's not clear whether it's only in your test code but busy-waiting like 
that will make your CPU very warm. :) Since requests cannot pass each 
other, your worker thread should have something like the following in 
that loop:

import core.thread;


Depending on how accurate the operating system honors your sleep 
requests (e.g. is it real-time?), you may want to sleep less than 
'duration' and then busy-wait the rest of the duration. Similar to the 
difference between spinForce() and yieldForce() of std.parallelism (I 
understand that your solution should not involve std.parallelism):


As an improvement when defining durations, you don't need to "hide" 
units in comments:

         // enum afterNmilliseconds = 1500;

         // Instead:
         enum after = 1500.msecs;

msecs and friends are defined in core.time:



More information about the Digitalmars-d-learn mailing list