Pthread_mutex_init Error Handling
Contents |
mutex SYNOPSIS #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;
pthread_mutex_init example DESCRIPTION The pthread_mutex_destroy() function shall destroy the mutex object referenced by pthread_mutex_init c mutex; the mutex object becomes, in effect, uninitialized. An implementation may cause pthread_mutex_destroy() to set the object
Pthread_mutex_init Attributes
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
Pthread_mutex_initializer Vs Pthread_mutex_init
are undefined. It shall be safe to destroy an initialized mutex that is unlocked. Attempting to destroy a locked mutex, or a mutex that another thread is attempting to lock, or a mutex that is being used in a pthread_cond_timedwait() or pthread_cond_wait() call by another thread, results in undefined behavior. The pthread_mutex_init() function shall initialize the pthread_mutex_init man 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. See Synchronization Object Copies and Alternative Mappings for further requirements. 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. 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. The behavior is undefined if the value specified by the mutex argument to pthread_mutex_destroy() does not refer to an initialized mutex. The behavior is undefined if the value specified by the attr argument to pthread_mutex_init() does not refer to an initialized mutex attributes object. RETURN VALUE If successful, the pthread_mutex_destroy() and pthread_mutex_init() functions shall return zero;
the interface may not be implemented on Linux. Name pthread_mutex_destroy, pthread_mutex_init - destroy and initialize a
Pthread_mutex_init Recursive
mutex Synopsis #include
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this http://stackoverflow.com/questions/14320041/pthread-mutex-initializer-vs-pthread-mutex-init-mutex-param site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PTHREAD_MUTEX_INITIALIZER pthread_mutex_init error vs pthread_mutex_init ( &mutex, param) up vote 41 down vote favorite 11 Is there any difference between pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; Or pthread_mutex_t lock; pthread_mutex_init ( &lock, NULL); Am I safe enough if I use only the first method ? NOTE: My question mostly refers to very small programs where at the most what I'll do is connect several clients to a server and pthread_mutex_init error handling resolve their inquiries with worker threads. c ubuntu pthreads mutex share|improve this question edited Jan 14 '13 at 14:16 asked Jan 14 '13 at 14:10 Kalec 85131832 add a comment| 3 Answers 3 active oldest votes up vote 36 down vote accepted By older versions of the POSIX standard the first method with an initializer is only guaranteed to work with statically allocated variables, not when the variable is an auto variable that is defined in a function body. Although I have never seen a platform where this would not be allowed, even for auto variables, and this restriction has been removed in the latest version of the POSIX standard. The static variant is really preferable if you may, since it allows to write bootstrap code much easier. Whenever at run time you enter into code that uses such a mutex, you can be assured that the mutex is initialized. This is a precious information in multi-threading context. The method using an init function is preferable when you need special properties for your mutex, such as being recursive e.g or being shareable between processes, not only between threads. share|improve