Re: wait for a condition to be satisfied

On Mon, Jun 04, 2001 at 03:42:41PM +0200, Ronald Bultje wrote:
> Hi there,
> I'm looking for a function which returns if a certain condition is
> satisfied. I'm now using:
> /* now, we need to pause until scene detection is done */
> while (scene_detection_reply == 1)
> 	usleep(100000);
> but this is ugly (imho, that is ;-) ). Is there something like
> waitfor(scenedetection != 1) in Gtk, glib or anything?

Well in a normal program this kind of problem doesn't come up, since it has a single thread of execution.
It does in threaded programs though, and pthreads condition variables probably fit the bill nicely ;)
If your code snippet is waiting for something even more external to modify the variable (variable lives in shared memory perhaps), then you'd have to roll your own mechanism from pipes or signals or something.
If you don't wanna poll (sleep,test,sleep,test etc), it basically comes down to getting the thread that changed the variable, to notify the thread that's waiting for the variable to be changed.

> Secondly, sleep() (and usleep()) are not thread-safe, are they? is there
> anything thread-safe as a replacement for sleep()? (I heard about g_sleep
> but "grep -r g_sleep /usr/include/*" gives no results)

Dahm, I heard something like this but I can't remember or find anything in the pthreads docs. I don't program in threads.. I can do a better job of scheduling my simultaneous tasks than the linux kernel any day ;-)

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]