Program exiting from thread with infinite loop

Chris M. chrismohrfeld at comcast.net
Fri Jan 19 20:40:36 UTC 2018


On Friday, 19 January 2018 at 20:35:14 UTC, Chris M. wrote:
> On Friday, 19 January 2018 at 18:18:31 UTC, Ali Çehreli wrote:
>> On 01/19/2018 09:46 AM, Chris M. wrote:
>>
>> > I tried putting an infinite loop inside main() as well,
>> didn't seem to
>> > help.
>>
>> Another reason is an exception thrown in the child thread. If 
>> the exception is not caught, it will terminate the child 
>> thread and the main will not know anything about it. I have 
>> something about that here:
>>
>>
>> http://ddili.org/ders/d.en/concurrency.html#ix_concurrency.exception,%20concurrency
>>
>> > I'm guessing there's probably a better way to make the main
>> thread
>> > wait?
>>
>> std.core.thread_joinAll is a way but you would want to tell 
>> the other thread to terminate first.
>>
>>   https://dlang.org/phobos/core_thread.html#.thread_joinAll
>>
>> So, instead of while(true), try while(someCondition). In that 
>> case the main thread sets the condition e.g. by sending a 
>> message and then joins.
>>
>> Ali
>
> I'm not sure what's going on. I've tried all sorts of things 
> from your link, it just runs the function once and exits every 
> time. There aren't any exceptions being thrown from the thread.
>
> That robot example is basically exactly what I'm doing, and yet 
> even that doesn't seem to resolve it.

What I tried

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
         ownerTid.send("tmp");
     }
}

void main()
{
     spawn(&deviceDownloader);

     while (true)
     {
         auto msg = receiveOnly!string();
     }
}


More information about the Digitalmars-d-learn mailing list