Implementing a timer using threads
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Tue Jan 30 10:40:45 PST 2007
BCS wrote:
> Max Samukha wrote:
>> A platform independent way to put a thread to sleep using Phobos:
>> import std.c.time;
>>
>> sleep(1);// secs
>> msleep(1000); millisecs
>> http://www.digitalmars.com/d/archives/digitalmars/D/29144.html
>>
>>
>
> However that gives the problem of not accounting for the run time of the
> threads "action". Not alwyas a problem, but...
>
> What is thread suposed to do to "kill time"? e.i. let other things run
> with out using up much CPU but keep poling the thread.
>
> while(NothingToDo())
> thisThread.MarkTime();
Couldn't you just do something like:
-----
time_t nextEvent = currentTime() + interval;
time_t now;
while (true) {
while ((now = currentTime()) < nextEvent)
sleep(nextEvent - now); // or msleep, if you prefer
nextEvent += interval;
action();
}
-----
(where time_t is some type suitable for measuring time)
That should sleep until it's time, right?
More information about the Digitalmars-d-learn
mailing list