Home > pthread join > error return code from pthread_join is 3

Error Return Code From Pthread_join Is 3

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 of c++ pthread join this site About Us Learn more about Stack Overflow the company Business Learn man pthread join more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question pthread join all threads x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up

What Does Pthread Join Do

pthread_join doesn't work up vote 0 down vote favorite What I want to accomplish: In the main function are created two threads. They increment a global variable with the number 5. And send a signal to consumer thread that decrements the variables.In the consumer thread between each decrementation the current value is displayed. The main thread has to wait until all the threads pthread_join example in c are finished and then exit. What I get: Some times the main function exits before the consumer had a chance to display the results. I'm using pthread_join, but it returns error code 3. Any Ideas how to get the wanted results? The code is bellow. #include #include #include #include static pthread_mutex_t mtx; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; void *producer(void *arg); void *consumer(void *arg); static int avail = 0; int main(int argc, char *argv[]){ pthread_t cons1, prod1, prod2; int status; int t1; int t2; int t3; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_mutexattr_t mtxAttr; pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK); pthread_mutex_init(&mtx, &mtxAttr); t1 = pthread_create(&prod1, &attr, producer, NULL); if(t1 != 0){ perror("problem1"); } t2 = pthread_create(&prod2, &attr, producer, NULL); if(t2 != 0){ perror("problem2"); } t3 = pthread_create(&cons1, &attr, consumer, NULL); if(t3 != 0){ perror("problem3"); } status = pthread_join(t1, NULL); if(status != 0){ perror("can't join1"); } status = pthread_join(t2, NULL); if(status != 0){ perror("can't join2"); printf("\n%d\n", status); } status = pthread_join(t3, NULL); if(status != 0){ printf("%s",strerror(errno)); } printf("\nend result \t%d\n",avail); printf("fin\n"); //while(1){} return 0; } void *producer(void *arg){ int s; printf("producer\n"); s = pthread_mutex_lock(&mtx); avail+=5; s = pthread_mutex_unlock(&mtx); s = pthread_cond_si

for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately. The thread specified by thread must be joinable. If retval is not NULL, then pthread_join() https://linux.die.net/man/3/pthread_join copies the exit status of the target thread (i.e., the value that https://www.sourceware.org/pthreads-win32/manual/pthread_join.html the target thread supplied to pthread_exit(3)) into the location pointed to by *retval. If the target thread was canceled, then PTHREAD_CANCELED is placed in *retval. If multiple threads simultaneously try to join with the same thread, the results are undefined. If the thread calling pthread_join() is canceled, then pthread join the target thread will remain joinable (i.e., it will not be detached). Return Value On success, pthread_join() returns 0; on error, it returns an error number. Errors EDEADLK A deadlock was detected (e.g., two threads tried to join with each other); or thread specifies the calling thread. EINVAL thread is not a joinable thread. EINVAL Another thread is already waiting to error return code join with this thread. ESRCH No thread with the ID thread could be found. Conforming to POSIX.1-2001. Notes After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated. Joining with a thread that has previously been joined results in undefined behavior. Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread". Avoid doing this, since each zombie thread consumes some system resources, and when enough zombie threads have accumulated, it will no longer be possible to create new threads (or processes). There is no pthreads analog of waitpid(-1, &status, 0), that is, "join with any terminated thread". If you believe you need this functionality, you probably need to rethink your application design. All of the threads in a process are peers: any thread can join with any other thread in the process. Example See pthread_create(3). See Also pthread_cancel(3), pthread_create(3), pthread_detach(3), pthread_exit(3), pthread_tryjoin_np(3), pthreads(7) Referenced By cp_thread(3), pthread_attr_getdetachstate(3) Site Search Library linux docs linux man pages page load time Toys world sunlight moon phase trace explorer

join another thread without waiting Synopsis #include int pthread_join(pthread_t th, void **thread_return); int pthread_timedjoin_np(pthread_t th, void **thread_return, const struct timespec *abstime); int pthread_tryjoin_np(pthread_t th, void **thread_return); Description pthread_join suspends the execution of the calling thread until the thread identified by th terminates, either by calling pthread_exit(3) or by being cancelled. If thread_return is not NULL, the return value of th is stored in the location pointed to by thread_return. The return value of th is either the argument it gave to pthread_exit(3) , or PTHREAD_CANCELED if th was cancelled. The joined thread th must be in the joinable state: it must not have been detached using pthread_detach(3) or the PTHREAD_CREATE_DETACHED attribute to pthread_create(3) . When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore, pthread_join must be called once for each joinable thread created to avoid memory leaks. pthread_timedjoin_np is identical to pthread_join except that it will return the error ETIMEDOUT if the target thread th has not exited before abstime passes. If abstime is NULL the function will wait forever and it's behaviour will therefore be identical to pthread_join. pthread_tryjoin_np is identical to pthread_join except that it will return immediately with the error EBUSY if the target thread th has not exited. At most one thread can wait for the termination of a given thread. Calling pthread_join on a thread th on which another thread is already waiting for termination returns an error. Cancellation pthread_join, pthread_tryjoin_np and pthread_timedjoin_np are cancellation points. If a thread is cancelled while suspended in either function, the thread execution resumes immediately and the cancellation is executed without waiting for the th thread to terminate. If cancellation occurs during either function, the th thread remains not joined. Return Value On success, the return value of th is stored in the location pointed to by thread_return, and 0 is returned. On error, a non-zero error code is returned. Errors ESRCH No thread could be found corresponding to that specified by th. EINVAL The th thread has been detached. EINVAL Another thread is already waiting on termination of th. ETIMEDOUT (pthread_timedjoin_np only): abstime passed before th could be joined. EDEADLK The th argument refers to the calling t

 

Related content

pthread join error 35

Pthread Join Error p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and relatedl policies of this 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 million programmers just like you helping each other Join them it only takes a minute Sign up Does the pthreads functions set the errno internally