Friday, April 26, 2013

pthread_mutex_destroy example c c++


NAME

pthread_mutex_destroy, pthread_mutex_init - destroy and initialize a mutex

SYNOPSIS

[THR] [Option Start] #include <pthread.h>

int pthread_mutex_destroy(pthread_mutex_t *
mutex);
int pthread_mutex_init(pthread_mutex_t *restrict
 mutex,
       const pthread_mutexattr_t *restrict
 attr);
pthread_mutex_t
 mutex = PTHREAD_MUTEX_INITIALIZER; [Option End]

DESCRIPTION

The pthread_mutex_destroy() function shall destroy the mutex object referenced by mutex; the mutex object becomes, in effect, uninitialized. An implementation may cause pthread_mutex_destroy() to set the object referenced by mutex to an invalid value. A destroyed mutex object can be reinitialized using pthread_mutex_init(); the results of otherwise referencing the object after it has been destroyed are undefined.
It shall be safe to destroy an initialized mutex that is unlocked. Attempting to destroy a locked mutex results in undefined behavior.(pthread_mutex_destroy, pthread_mutex_init)
The pthread_mutex_init() function shall initialize the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; the effect shall be the same as passing the address of a default mutex attributes object. Upon successful initialization, the state of the mutex becomes initialized and unlocked.
Only mutex itself may be used for performing synchronization. The result of referring to copies of mutex in calls to pthread_mutex_lock(),pthread_mutex_trylock()pthread_mutex_unlock(), and pthread_mutex_destroy() is undefined.
Attempting to initialize an already initialized mutex results in undefined behavior.
In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.

RETURN VALUE of (pthread_mutex_destroy, pthread_mutex_init)

If successful, the pthread_mutex_destroy() and pthread_mutex_init() functions shall return zero; otherwise, an error number shall be returned to indicate the error.
The [EBUSY] and [EINVAL] error checks, if implemented, act as if they were performed immediately at the beginning of processing for the function and shall cause an error return prior to modifying the state of the mutex specified by mutex.

ERRORS

The pthread_mutex_destroy() function may fail if:
[EBUSY]
The implementation has detected an attempt to destroy the object referenced by mutex while it is locked or referenced (for example, while being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another thread.
[EINVAL]
The value specified by mutex is invalid.
The pthread_mutex_init() function shall fail if:
[EAGAIN]
The system lacked the necessary resources (other than memory) to initialize another mutex.
[ENOMEM]
Insufficient memory exists to initialize the mutex.
[EPERM]
The caller does not have the privilege to perform the operation.
The pthread_mutex_init() function may fail if:
[EBUSY]
The implementation has detected an attempt to reinitialize the object referenced by mutex, a previously initialized, but not yet destroyed, mutex.
[EINVAL]
The value specified by attr is invalid.
These functions shall not return an error code of [EINTR].
Example of (pthread_mutex_destroy, pthread_mutex_init)

 Without static initialization, a self-initializing routine foo() might look as follows:

static pthread_once_t foo_once = PTHREAD_ONCE_INIT;
static pthread_mutex_t foo_mutex;


void foo_init()
{
    pthread_mutex_init(&foo_mutex, NULL);
}


void foo()
{
    pthread_once(&foo_once, foo_init);
    pthread_mutex_lock(&foo_mutex);
   /* Do work. */
    pthread_mutex_unlock(&foo_mutex);
}

With static initialization, the same routine could be coded as follows:

static pthread_mutex_t foo_mutex = PTHREAD_MUTEX_INITIALIZER;

void foo()
{
    pthread_mutex_lock(&foo_mutex);
   /* Do work. */
    pthread_mutex_unlock(&foo_mutex);
}

struct obj {
pthread_mutex_t om;
    int refcnt;
    ...
};


obj_done(struct obj *op)
{
    pthread_mutex_lock(&op->om);
    if (--op->refcnt == 0) {
        pthread_mutex_unlock(&op->om);
pthread_mutex_destroy(&op->om);
free(op);
    } 
    else
pthread_mutex_unlock(&op->om);
}