[fpc-pascal] Solution for Timer in daemon

Michael Van Canneyt michael at freepascal.org
Sat Mar 2 10:41:23 CET 2013



On Sat, 2 Mar 2013, Ludo Brands wrote:

> On 03/01/2013 07:10 PM, Krzysztof wrote:
>> Hi,
>>
>> I'm reading that I can't use timer in daemon because daemon core is
>> based on thread.
>
> daemonapp is using threads but nothing stops you from daemonizing your
> app yourself with a simple fork.

The threads are not there for daemonizing. 
They are there because a single app can host multiple services (daemons) as on Windows.

>> So I'm trying to create another thread which simulate
>> timer. My interval is quite big (~1-5 minutes), so I can't just use
>> sleep(60000) because daemon will hung on terminate. So I have two ideas:
>>
>> 1. Create loop with short sleep(1000) which on each loop check if main
>> interval occur and check if daemon is terminated
>> 2. Create loop with RTL event with RtlEventWaitFor(Event, 60000) and
>> daemon on terminate just send event to worker so it immediately exit.
>>
>> What is the best efficient solution? Maybe exists another way?
>>
>
> Very difficult to give an absolute answer without knowing what the
> daemon is doing.
>
> On unix, an alternate solution is to use fpSelect (Function
> fpSelect(N:cint;readfds,writefds,exceptfds:pfdSet;TimeOut:PTimeVal):cint;
> ) and specify a time-out value.

I would go for this.

Michael.



More information about the fpc-pascal mailing list