Home > pthread mutex lock error > pthread_mutex_lock error checking

Pthread_mutex_lock Error Checking

Contents

pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); DESCRIPTION The mutex object referenced by mutex is locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread blocks until the mutex becomes available. This operation returns with the mutex object referenced by pthread_mutex_lock example mutex in the locked state with the calling thread as its owner. If the pthread_mutex_lock return value mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection is not provided. Attempting to relock the mutex causes deadlock. If a thread attempts to man pthread_mutex_lock unlock a mutex that it has not locked or a mutex which is unlocked, undefined behaviour results. If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking is provided. If a thread attempts to relock a mutex pthread_mutex_trylock that it has already locked, an error will be returned. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error will be returned. If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex maintains the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count is set to one. Every time a

Pthread_mutex_init

thread relocks this mutex, the lock count is incremented by one. Each time the thread unlocks the mutex, the lock count is decremented by one. When the lock count reaches zero, the mutex becomes available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error will be returned. If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results in undefined behaviour. Attempting to unlock the mutex if it was not locked by the calling thread results in undefined behaviour. Attempting to unlock the mutex if it is not locked results in undefined behaviour. The function pthread_mutex_trylock() is identical to pthread_mutex_lock() except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call returns immediately. The pthread_mutex_unlock() function releases the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex's type attribute. If there are threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock() is called, resulting in the mutex becoming available, the scheduling policy is used to determine which thread shall acquire the mutex. (In the case of PTHREAD_MUTEX_RECURSIVE mutexes, the mutex becom

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 site About Us Learn more about Stack Overflow the company Business

Pthread_mutex_initializer

Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation pthread_mutex_unlock Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like pthread_mutex_recursive you, helping each other. Join them; it only takes a minute: Sign up pthread_mutex_lock return not tested up vote 7 down vote favorite 1 I'm really wondering why all source codes that implement a pthread_mutex_lock never test http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_mutex_lock.html its return value as defined : documentation of pthread even in books the examples don't test if the lock is in error, codes just do the lock. Is there any reason I missed to let it untested ? multithreading coding-style pthreads share|improve this question asked Jun 9 '11 at 9:07 dlewin 478723 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted Basically, the only “interesting” error is EINVAL, which in http://stackoverflow.com/questions/6290461/pthread-mutex-lock-return-not-tested most programs will only happen because of memory corruption, or, as I know from my own painful experience, during program shutdown after destructors have already destroyed some mutexes. The way I see it, the only reasonable response to such an error is to abort the program, which on the other hand is very inconvenient if the errors occur precisely because the program is already shutting down. Of course, this can be solved, but it’s not at all that simple, and not much is gained by it for most programs. share|improve this answer answered Jun 9 '11 at 10:26 Ringding 2,6661010 Then you don't handle mem corruption ? –dlewin Jun 9 '11 at 14:24 5 No, you don't "handle" memory corruption, you just don't corrupt memory in the first place. –Ringding Jun 9 '11 at 16:13 1 Actually, my question was more about EBUSY or EDEADLK, respectively : "The mutex could not be acquired because it was already locked" and "A deadlock condition was detected or the current thread already owns the mutex": what make you think this can't happen ? –dlewin Jun 14 '11 at 11:10 EBUSY is not a documented error condition of pthread_mutex_lock, while EDEADLK does only happen if you have explicitly set the mutex' type to error checking (PTHREAD_MUTEX_ERRORCHECK). –Ringding Jun 14 '11 at

= PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t recmutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; pthread_mutex_t errchkmutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); int pthread_mutex_destroy(pthread_mutex_t http://manpages.ubuntu.com/manpages/wily/man3/pthread_mutex_lock.3.html *mutex); DESCRIPTION A mutex is a MUTual EXclusion device, and is useful for protecting shared data structures from concurrent modifications, and implementing critical sections and monitors. A mutex has two possible states: unlocked (not owned by any thread), and locked (owned by one thread). A mutex can pthread_mutex_lock error never be owned by two different threads simultaneously. A thread attempting to lock a mutex that is already locked by another thread is suspended until the owning thread unlocks the mutex first. pthread_mutex_init initializes the mutex object pointed to by mutex according to the mutex attributes specified pthread_mutex_lock error checking in mutexattr. If mutexattr is NULL, default attributes are used instead. The LinuxThreads implementation supports only one mutex attributes, the mutex kind, which is either ``fast'', ``recursive'', or ``error checking''. The kind of a mutex determines whether it can be locked again by a thread that already owns it. The default kind is ``fast''. See pthread_mutexattr_init(3) for more information on mutex attributes. Variables of type pthread_mutex_t can also be initialized statically, using the constants PTHREAD_MUTEX_INITIALIZER (for fast mutexes), PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP (for recursive mutexes), and PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP (for error checking mutexes). pthread_mutex_lock locks the given mutex. If the mutex is currently unlocked, it becomes locked and owned by the calling thread, and pthread_mutex_lock returns immediately. If the mutex is already locked by another thread, pthread_mutex_lock suspends the calling thread until the mutex is unlocked. If the mutex is already locked by the

 

Related content

pthread_mutex_lock error numbers

Pthread mutex lock Error Numbers table id toc tbody tr td div id toctitle Contents div ul li a href Pthread mutex trylock a li li a href Pthread mutex unlock a li li a href Pthread mutex lock Error a li ul td tr tbody table p - lock and unlock a mutex SYNOPSIS tt sup a href javascript open code 'THR' THR a sup img src images opt-start gif alt Option Start border include a href basedefs pthread h html pthread h a br br int pthread mutex lock pthread mutex t tt i mutex i tt br

pthread_mutex_lock error handling

Pthread mutex lock Error Handling table id toc tbody tr td div id toctitle Contents div ul li a href Pthread mutex lock Return Value a li li a href What Is Pthread mutex lock a li li a href Man Pthread mutex lock a li ul td tr tbody table p pthread mutex lock pthread mutex t i mutex i int pthread mutex trylock pthread mutex t i mutex i int pthread mutex unlock pthread mutex t i mutex i DESCRIPTION The mutex object referenced by mutex is locked by calling pthread mutex lock If the mutex is already

pthread_mutex_lock error 35

Pthread mutex lock Error table id toc tbody tr td div id toctitle Contents div ul li a href Pthread mutex lock Return Value a li li a href Pthread mutex init a li li a href Pthread mutex unlock a li ul td tr tbody table p p p p p p p p

pthread_mutex_lock error codes

Pthread mutex lock Error Codes table id toc tbody tr td div id toctitle Contents div ul li a href Pthread mutex lock Return Value a li li a href Pthread mutex trylock a li li a href Pthread mutex init a li li a href Pthread mutex initializer a li ul td tr tbody table p pthread mutex lock pthread mutex t i mutex i int pthread mutex trylock pthread mutex t i mutex i int pthread mutex unlock pthread mutex t i mutex i DESCRIPTION The mutex object referenced by mutex is locked by calling pthread mutex lock

pthread_mutex_lock error code 22

Pthread mutex lock Error Code table id toc tbody tr td div id toctitle Contents div ul li a href Mutex Lock Failed a li li a href Pthread Mutex Example a li li a href Pthread mutex initializer a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions relatedl you might have Meta Discuss the workings and policies pthread mutex lock mutex failed with error of this site About Us Learn more about Stack Overflow the company Business p h id Mutex Lock Failed p Learn