Function Macro

GLibRW_LOCK_READER_AUTO_LOCK

unstable since: 2.80.0

Declaration

#define G_RW_LOCK_READER_AUTO_LOCK (
  mutex,
  var
)

Description

Declare a GRWLockReaderLocker variable with g_autoptr() and lock for reading. 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_RW_LOCK_READER_AUTO_LOCK (&obj->rw_lock, locker);

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

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

Available since: 2.80.0

This function is not directly available to language bindings.

Parameters

mutex

Type: -

A GRWLock

var

Type: -

A variable name to be declared.