Doubts about GThreads

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

 



Hello:

Recently I have written an application using the threads from glib.

First I will explain my situation. Actually my application is using
three threads, the main one and two created by this one. In a certain
moment I stop the two waiting for a certain condition.

The first thread using it own mutex and a condition shared by them all.

	g_mutex_lock (first_mutex);
	g_cond_wait (cond, first_mutex);
	g_mutex_unlock (first_mutex);

The same for the second mutex:

	g_mutex_lock (second_mutex);
	g_cond_wait (cond, second_mutex);
	g_mutex_unlock (second_mutex);

Then in the main thread for certain moment:

	g_mutex_lock (main_mutex);
	g_cond_broadcast (cond);
	g_mutex_unlock (main_mutex);

When both threads are sleep, and main calls to g_cond_broadcast is
called, only one thread is waked up. ¿ shouldn't both threads be waked
up ?

I have solved this using different GCond, one for each thread, and
waking up them all one by one, but I don't think it's right.

About the other question, after being reading the docs, I have found
this.

> g_cond_broadcast ()
> void                g_cond_broadcast                    (GCond *cond);
> 
> If threads are waiting for cond, all of them are woken up. It is good
> practice to lock the same mutex as the waiting threads, while calling
> this function, though not required.

"It is good practice to lock the same mutex as the waiting threads.". Is
this possible ?

I mean, before waiting on a condition, is usual to lock the mutex used
on the condition, so if the thread is waiting and the mutex locked,
another thread will not be able to lock the same mutex to wake up the
thread with cond, blocking each other.

Thank you,
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list


[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux