The GMutex struct is an opaque data structure to represent a mutex
(mutual exclusion). It can be used to protect data against shared
access. Take for example the following function:
It is easy to see that this won't work in a multi-threaded
application. There current_number must be protected against shared
access. A first naive implementation would be:
This looks like it would work, but there is a race condition while
constructing the mutex and this code cannot work reliable. Please do
not use such constructs in your own programs! One working solution
is:
GStaticMutex provides a simpler and safer way of doing this.
If you want to use a mutex, and your code should also work without
calling g_thread_init() first, then you can not use a GMutex, as
g_mutex_new() requires that the thread system be initialized. Use a
GStaticMutex instead.
A GMutex should only be accessed via the following functions.
Note
All of the g_mutex_* functions are
actually macros. Apart from taking their addresses, you can however
use them as if they were functions.
The GMutex struct is an opaque data structure to represent a mutex (mutual exclusion). It can be used to protect data against shared access. Take for example the following function: It is easy to see that this won't work in a multi-threaded application. There current_number must be protected against shared access. A first naive implementation would be: This looks like it would work, but there is a race condition while constructing the mutex and this code cannot work reliable. Please do not use such constructs in your own programs! One working solution is: GStaticMutex provides a simpler and safer way of doing this. If you want to use a mutex, and your code should also work without calling g_thread_init() first, then you can not use a GMutex, as g_mutex_new() requires that the thread system be initialized. Use a GStaticMutex instead. A GMutex should only be accessed via the following functions. Note All of the g_mutex_* functions are actually macros. Apart from taking their addresses, you can however use them as if they were functions.