Program exiting from thread with infinite loop

Chris M. chrismohrfeld at comcast.net
Fri Jan 19 17:05:44 UTC 2018


I have the following that is supposed to pull and store info from 
a server every five minutes. It works if I move the body of 
deviceDownloader into main(). However when I try to spawn it in 
another thread it runs the function and then ends once it's done, 
like it's ignoring the while(true). I tried with std.parallelism, 
core.thread, vibe.d's concurrency library, but get the same 
result every time. Any idea how to better accomplish this?


import std.net.curl;
import device;
import std.datetime.systime;
impor std.concurrency;

void deviceDownloader()
{
     auto APIServer = HTTP();
     APIServer.addRequestHeader("X-Auth-Token:", AuthToken);
     APIServer.onProgress = delegate int(size_t dl, size_t dln, 
size_t ul, size_t uln)
     {
         if (dl != 0)
             write("Progress: downloaded ", dln, " of ", dl, "\r");

         return 0;
     };

     while (true)
     {
         auto currentTime = Clock.currTime;
         if (currentTime.minute % 5 == 0 && currentTime.second == 
0)
             retrieveDevices(URL, APIServer); // info retrieved 
and stored here
     }
}

void main()
{
     spawn(&deviceDownloader);
}


More information about the Digitalmars-d-learn mailing list