Home > pthread mutex lock error > pthread_mutex_lock error code 22

Pthread_mutex_lock Error Code 22

Contents

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies pthread_mutex_lock(&mutex)failed with error 22 of this site About Us Learn more about Stack Overflow the company Business

Mutex Lock Failed

Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges

Pthread Mutex Example

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:

Pthread_mutex_initializer

Sign up pthread_mutex_lock returns 22 in C [duplicate] up vote 1 down vote favorite This question already has an answer here: pthread_mutex_lock returns invalid argument 2 answers I'm learning how to use Pthread in C. I've tried to use pthread_mutex_lock. It is supposed to return 0 when the locking is successful. But my program always returns 22 - invalid argument. The code is pthread_mutex_unlock as follow: pthread_mutex_lock is used in work_function #include #include #include #include #define N 2 void *work_function(void *param); int count=0; pthread_mutex_t mut; int main(int args, char **argv) { pthread_t tid[N]; long int iterations = atoi(argv[1]); pthread_create(&tid[0], NULL, work_function, (void *) iterations); pthread_create(&tid[1], NULL, work_function, (void *) iterations); pthread_join(tid[1], NULL); pthread_join(tid[0], NULL); if (count != iterations * 2) printf("Error: %d\n",count); else printf("Value as expected: count = %d\n", count); pthread_exit(NULL); } void *work_function(void *param) { long int max_iter = (long int) param; int i, tmp; pid_t pid = getpid(); pthread_t id = pthread_self(); i = pthread_mutex_lock(&mut); printf("%d\n", i); for(i = 0; i < max_iter; i++) { tmp = count; tmp ++; printf("in thread: pid=%d and id=%u count=%d new\n", (unsigned int) pid, (unsigned int) id, count); // sleep(1); count = tmp; printf("haha\n"); } pthread_mutex_unlock(&mut); pthread_exit(NULL); } The following result is produced when 3 is passed in as the command line argument. 22 22 in thread: pid=36767 and id=6819840 count=0 new in thread: pid=36767 and id=7356416 count=0 new haha haha in thread: pid=36767 and id=6819840 count=1 new in thread: pid=36767 and id=7356416 count=1 new haha h

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 pthread_mutexattr_t Us Learn more about Stack Overflow the company Business Learn more about hiring pthread_mutex_destroy developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the std::mutex 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 c++ correct use of mutex up http://stackoverflow.com/questions/30511046/pthread-mutex-lock-returns-22-in-c vote 3 down vote favorite i have multithreaded project and i've run it through valgrind with --tool=helgrind and it showed me few errors. I'm using mutex there exaxtly how i found on the net how to use it, can you please show me whats wrong? #include #include #define MAX_THREADS 100 #define MAX_SESSIONS 100 static pthread_mutex_t M_CREATE_SESSION_LOCK= PTHREAD_MUTEX_INITIALIZER; ..... void connection::proccess(threadVarsType &THREAD) { .... pthread_mutex_lock(&M_CREATE_SESSION_LOCK); http://stackoverflow.com/questions/5836538/c-correct-use-of-mutex unsigned int ii; for (ii=0; ii

- lock and unlock a mutex SYNOPSIS [THR] [Option Start] #include <pthread.h>

int pthread_mutex_lock(pthread_mutex_t *
mutex);
int pthread_mutex_trylock(pthread_mutex_t *
mutex);
int pthread_mutex_unlock(pthread_mutex_t *
mutex); [Option End] DESCRIPTION http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_lock.html The mutex object referenced by mutex shall be locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread shall block until the mutex becomes available. This operation shall return with the mutex object referenced by mutex in the locked state with the calling thread as its owner. [XSI] If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not be provided. Attempting to relock the mutex causes pthread_mutex_lock error deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results. If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking shall be provided. If a thread attempts to relock a mutex that it has already locked, an error shall be returned. If a thread attempts to unlock a mutex that it has not locked or a mutex pthread_mutex_lock error code which is unlocked, an error shall be returned. If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex shall maintain the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count shall be set to one. Every time a thread relocks this mutex, the lock count shall be incremented by one. Each time the thread unlocks the mutex, the lock count shall be decremented by one. When the lock count reaches zero, the mutex shall become 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 shall be returned. If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results in undefined behavior. Attempting to unlock the mutex if it was not locked by the calling thread results in undefined behavior. Attempting to unlock the mutex if it is not locked results in undefined behavior. The pthread_mutex_trylock() function shall be equivalent 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 shall return immediately. If the mutex type is PTHREAD_MUTEX_RECURSIVE and the mutex is curren

 

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 checking

Pthread mutex lock Error Checking table id toc tbody tr td div id toctitle Contents div ul 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 If the mutex is already locked the calling thread blocks until the mutex becomes relatedl available This operation