Function Macro

GLibMUTEX_AUTO_LOCK

since: 2.80.0

Declaration

#define G_MUTEX_AUTO_LOCK (
  mutex,
  var
)

Description

Declare a GMutexLocker variable with g_autoptr() and lock the mutex. The mutex will be unlocked automatically when leaving the scope. The variable is declared with G_GNUC_UNUSED to avoid compiler warning if it is not used in the scope.

This feature is only supported on GCC and clang. This macro is not defined on other compilers and should not be used in programs that are intended to be portable to those compilers.

Note that this should be used in a place where it is allowed to declare a variable, which could be before any statement in the case -Wdeclaration-after-statement is used, or C standard prior to C99.

{
  G_MUTEX_AUTO_LOCK (&obj->mutex, locker);

  obj->stuff_with_lock ();
  if (condition)
    {
      // No need to unlock
      return;
    }

  // Unlock before end of scope
  g_clear_pointer (&locker, g_mutex_locker_free);
  obj->stuff_without_lock ();
}

Available since: 2.80.0

This function is not directly available to language bindings.

Parameters

mutex

Type: -

A GMutex

var

Type: -

A variable name to be declared.