man rthread_mutex (Fonctions bibliothèques) - Roy threading API

NAME

rthread - Roy threading API

SYNOPSIS

#include <roy.h>

RThreadMutex rthread_mutex_new (void);

int rthread_mutex_lock (RThreadMutex mutex);

int rthread_mutex_trylock (RThreadMutex mutex);

int rthread_mutex_unlock (RThreadMutex mutex);

void rthread_mutex_destroy (RThreadMutex mutex);

RTHREAD_MUTEX_ENTER(mutex)

RTHREAD_MUTEX_EXIT(mutex)

DESCRIPTION

These functions add mutex support to the RThread API. Mutex stands for mutual exclusion and allows only one thread at a time to enter a peice of code protected by one. These are often used to protect global and static variables and data structures from simultaneous modification.

Mutex locking should always be balanced; that is, one lock for every unlock. While this is not a requirement on many pthread implementations, it is required on Win32.

The RTHREAD_MUTEX_ENTER/EXIT macros are to be used as a pair within a single function and require opening and closing braces around the code they are to protect. This gives a nice, forced balance to mutexes. For example:

RTHREAD_MUTEX_ENTER(mutex) { ... code ... } RTHREAD_MUTEX_EXIT(mutex);

rthread_mutex_new(3) initializes a mutex for use. Note the lack of a pointer for this type. This is intentional as the type is defined differently on different platforms. You must always declare the mutex without a pointer.

rthread_mutex_lock(3) locks the mutex so that no other threads may enter. This call may block until the previous thread has exited the mutex (if necessary).

rthread_mutex_trylock(3) will not block if the mutex is in use and will simply return non-zero in the event that the lock is already in use.

rthread_mutex_unlock(3) will unlock a mutex allowing others to enter.

rthread_mutex_destroy(3) will destroy a mutex and free any storage associated with it.

RETURN VALUES

All functions returning an int return 0 on success, or non-zero on failure.

SEE ALSO