Subject AW: [firebird-support] Re: DELAY OR WAIT
Author Alexander Gräf
> -----Ursprüngliche Nachricht-----
> Von: Adam [mailto:s3057043@...]
> Gesendet: Montag, 13. Dezember 2004 03:14
> An: firebird-support@yahoogroups.com
> Betreff: [firebird-support] Re: DELAY OR WAIT
>
>
> Triggers would probably be a better solution for him. I would
> be careful about using sleep, because from memory (please
> check this, I may be wrong but we did encounter this before),
> sleep acts on every thread of the program.

What do you mean by "using sleep"?

> If that is the case, then no other thread in firebird would
> receive CPU time, and the query it was waiting for would
> probably never finish, or at best take an awful long time.
> WaitForSingleObject has a timeout parameter, as well as only
> acting on the current thread. You could just wait for the
> firebird process or something like that (which will always
> timeout), and set the timeout to 1.5 seconds.

When using WaitForSingleObject with Timeout and an Object which never gets into singaled state and which never expires, it acts like Sleep. Sleep is only local to the thread where it is called, so that other threads in the same process and eventually other processes get CPU time. WaitForSingleObject with Timeout is what the name says: Waiting for some object to get singaled, without deadlocking the server, so you can specify the maximum time to wait. In this case, this function is not properly suited.

> I agree it is risky to sleep in a UDF even if it does work. I
> can see a couple of reasons why you would want it, but to me
> they would put too much resource demand on the server, and
> could easily be transferred to the client application to poll.

If the criteria which he is looking for is not satisfied, it will only get satisfied if someone inserts/updates/deletes data from one or more tables. One could simply attach triggers to these actions and then check for the condition to be satisfied. No need to loop until condition or true, or poll the server every n msecs.

Doing

While (!condition) Sleep(xy);
DoAction();

is bad programing style, even in C/C++. The only proper way to archive this is to use one of the signal objects. The main problem is: If xy is too long, the loop will be exited to late, or maybe the condition could change to unsatisfied again *before* the while-loop checks for it again, and so hitting the condition could be missed. If xy is too small, the thread will take too much CPU time, even when using Sleep.

Regards. Alex