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
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