Re: How to safely release a mutex?
- From: "Lieven van der Heide" <lievenvanderheide gmail com>
- To: "Dave Benson" <daveb idealab com>
- Cc: gtk-list gnome org
- Subject: Re: How to safely release a mutex?
- Date: Thu, 13 Nov 2008 19:47:37 -0800
Well, not the answer you might like, but if you encounter a situation
where this might be a problem, you really have a flaw somewhere else
in your system. Just think of your mutex as any other type of data,
and you'll see that it's of course not allowed to free an object
that's still in use by another thread.
On Thu, Nov 13, 2008 at 7:25 PM, Dave Benson <daveb idealab com> wrote:
> everyone knows the answer; that's why they don't reply.
> glib follows pthread more-or-less, g_mutex_free maps to
> pthread_mutex_destroy whose man page says:
>
> pthread_mutex_destroy destroys a mutex object, freeing the resources it
> might hold. The mutex must be unlocked on entrance. In the LinuxThreads
> implementation, no resources are associated with mutex objects, thus
> pthread_mutex_destroy actually does nothing except checking that the
> mutex is unlocked.
>
> Your code idea:
>> g_mutex_lock(mutex);
>> g_mutex_unlock(mutex);
>> g_mutex_free(mutex);
> may "fix" your situation, but it is almost certainly wrong.
> if you are already holding the lock,
> which is rather likely the case, then that's deadlock,
> your program will freeze.
> if you aren't, then you've introduced a race condition:
> after unlocking, another thread may lock the mutex
> and g_mutex_free will be unhappy.
>
> threads are difficult.
>
> - dave
>
> On Fri, Nov 14, 2008 at 11:01:11AM +0800, Peter Cai wrote:
>> 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?
>> >
>>
>>
>>
>> --
>> ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
>> _______________________________________________
>> gtk-list mailing list
>> gtk-list gnome org
>> http://mail.gnome.org/mailman/listinfo/gtk-list
>
> --
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]