A scheduled control signal with fibers?

Ferhat Kurtulmuş aferust at gmail.com
Sun Sep 27 10:06:16 UTC 2020


On Friday, 25 September 2020 at 13:37:09 UTC, Steven 
Schveighoffer wrote:

> Given the rate and the number of concurrent tasks, I'd say 
> threads.
>
> -Steve

Here is my testable and minimal code using 1 extra thread. Thank 
you all!

import core.thread;

import std.stdio;
import std.concurrency;
import std.container.dlist;
import std.datetime;
import std.datetime.systime;

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

struct Entry {
     SysTime st;
     int val;
}

void main() {

     spawn(&worker);

     while (true) {
         int v;

         "enter your value: ".write; // 
getFlameIntensityViaImageProcessing()
         readf(" %d", &v);

         if(v==0){
             shouldRun = false;
             break;
         }


         queue.insertFront(Entry(Clock.currTime + 1500.msecs, v));
     }

     writeln("main is done.");
}

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);
         }
     }
}


More information about the Digitalmars-d-learn mailing list