Re: How to safely release a mutex?



As no body seems to know the answer, I made a test:

============================
#include <glib.h>

int main()
{
	if (!g_thread_supported ()) g_thread_init (NULL);

	GMutex    *mutex = g_mutex_new();
	g_mutex_lock(mutex);
	g_mutex_free(mutex);
	return 0;
}
============================

It crashes on g_mutex_free without any hesitate :)

So finally the approach I use to avoid this kind of problem is using
another mutex, which does not need to be freed,
to protect the one I will free.

But I am wondering if that was needed.   Can I just use:

	g_mutex_lock(mutex);
	g_mutex_unlock(mutex);
	g_mutex_free(mutex);

If thread switches just after unlock and another thread locked mutex,
the code might fail.

On Thu, Nov 13, 2008 at 4:41 PM, Peter Cai <newptcai gmail com> wrote:
> Hi all,
>
> Is it safe to call g_free_mutex on a locked mutex?
>
> If it's not, what is the correct way to do that?
>



-- 
而如何让自己的内心产生力量,在于自己而不在于别人。等到一跃而过,回头去看的时候,一个人因此获得了宝贵的经验和自信,下一次就可以面对更宽更深的壕沟。关键在于有多少意愿去面对这种犹豫时刻,因为大部分情况下,我们可以选择绕路的方法而回避这种艰难的选择。


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