GThread and GCond



Hi,

I hope you can help me ;)

I'm working with two threads. One of them is running in an infinite loop
(well, it finishes under certain conditions)
What I want to do is that the infinite loop pauses some times. So I create a
GCond, but I think I didn't understand how it works.
I do something like this:

Thread 1:

for (;;){
  g_mutex_lock(my_mutex);
  g_cond_wait(my_cond,mi_mutex);
  do_some_work_here....
  g_mutex_unlock(mi_mutex);
}

The Thread 2 asynchronously toggles the value of a variable between 0 and 1.
When the value is 1, I want the loop runs continuously, but when it is 0,
the loop stops.

if (value==0){
        value=1;
        g_cond_signal(my_cond);
    }
    else
        value=0;

I've read this:
http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html#g-thread-create
"The GCond<http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html#GCond>struct
is an opaque data structure to represent a condition"

So, the GCond represents the condition, and are not the condition itself...
So when I use g_cond_signal I think I'm telling "the condition is true,
unlock". But in the next iteration, the loop locks again.

How can I tell "The condition is STILL true..."?

Thanks in advance and apologyze if I was not clear explaining my problem.



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