Re: Glib mutex doesn't work on Windows



Hi

On 12/16/2010 07:47 PM, Michael T. wrote:
Hi,
  I'm using glib mutexes on Windows and I think they don't work.
I tried to use WIN API mutexes instead and they did work.

Short sample program:
I initialize the WIN API mutex, then create two threads using WIN API with this simple code:

DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
     int i = 0;
     for(i=0; i<30; i++)
     {
         WaitForSingleObject( Mut, INFINITE);

         printf("Jozko jolko %d, thread %d\n", i, GetCurrentThreadId()) ;

         ReleaseMutex(Mut);
     }
     return 0;
}

Without the mutexes the output is messy, because the console is a critical section, and unsynchronized 
threads create mess. With mutexes applied the output is nice and clean.
When I try the Glib mutex with the same code, it doesn't work and output remains messy.

The mutex initialization looks like this:

GStaticMutex dataMutex = G_STATIC_MUTEX_INIT;

The function:

DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
   int i = 0;

     for(i=0; i<30; i++)
     {

         g_static_mutex_lock(&dataMutex);

         printf("Jozko jolko %d, thread %d\n", i, GetCurrentThreadId()) ;

         g_static_mutex_unlock(&dataMutex);

     }
     return 0;
}

I used the GStaticMutex according the example code here:
http://library.gnome.org/devel/glib/2.17/glib-Threads.html#g-static-mutex-lock

There's no problem with compiling, I also don't get any error during execution. I am using MinGW compiler to 
create the binaries.
Why does it not work?
I don't know what version of windows you are using but:

There are two implementation in glib for mutual exclusion

http://world.std.com/~jmhart/csmutx.htm

One is this one:

static void
g_mutex_lock_win32_cs_impl (GMutex *mutex)
{
  EnterCriticalSection (*(CRITICAL_SECTION **)mutex);
}

static gboolean
g_mutex_trylock_win32_cs_impl (GMutex * mutex)
{
  return try_enter_critical_section (*(CRITICAL_SECTION **)mutex);
}

static void
g_mutex_unlock_win32_cs_impl (GMutex *mutex)
{
  LeaveCriticalSection (*(CRITICAL_SECTION **)mutex);
}

and the other is this one:

static void
g_mutex_lock_win32_impl (GMutex *mutex)
{
  WaitForSingleObject (*(HANDLE *) mutex, INFINITE);
}

static gboolean
g_mutex_trylock_win32_impl (GMutex * mutex)
{
  DWORD result;
  win32_check_for_error (WAIT_FAILED !=
                         (result = WaitForSingleObject (*(HANDLE *)mutex, 0)));
  return result != WAIT_TIMEOUT;
}

static void
g_mutex_unlock_win32_impl (GMutex *mutex)
{
  ReleaseMutex (*(HANDLE *) mutex);
}

So I suppose that you are using the first one. Can you try your example with Leave and Enter Critical section?
Maybe we need some function to force the using of one model instead of the other one

Michael Trimarchi

Thanks

myso
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list





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