Home > pthread join > pthread join error 35

Pthread Join Error 35

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 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 Does the pthreads functions set the errno internally? up vote 2 down vote favorite 1 In the below code i am calling pthread_join() with thread id as self. Result is that it returns error number 35. So same i am trying to print with perror. But it is displaying "success". My doubt is does library/system calls need to explictly set the errno for any errors or Did i miss any thing ? #include #include #include #include #include #include #define DEATH(mess) { perror(mess); exit(errno); } static void * threadFunc(void *arg) { void *res; printf("sleeping for 2 sec ...\n"); sleep(2); char *s = (char *) arg; pthread_t t = pthread_self(); int relval = pthread_join(t, &res); if (relval) perror("deadlock"); printf("return value is %d .....\n",relval); return (void *) strlen(s); } int main(int argc, char *argv[]) { pthread_t t1; void *res; int ret; ret = pthread_create(&t1, NULL, threadFunc, "Hello world\n"); if (ret != 0) DEATH ("pthread_create"); printf("Message from main()\n"); pthread_exit(&res); exit(EXIT_SUCCESS); } o/p Message from main() sleeping for 2 sec ... deadlock: Success return value is 35 ..... c linux pthreads share|improve this question asked Feb 9 '13 at 17:08 Goutham 1451314 2 Why don't you read the man page? –Mahmoud Al-Qudsi Feb 9 '13 at 19:32 That's not a doubt, it's a question. –Jonathan Wakely Feb 15 '13 at 0:53 add a comment| 3 Answers 3 active oldest votes up vote 6 down vote There is no requirement that thesr functions set errno or that they leave it alone. You're always free to do: errno = pthread_join(t, &res); share|improve this answer answered Feb 9 '13 at 19:12 R.. 127k15190421 Thanks -- this made me realize I can also use this with perror, since (on my system) all the codes pthread_join and

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 http://stackoverflow.com/questions/14790535/does-the-pthreads-functions-set-the-errno-internally 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 https://www.sourceware.org/pthreads-win32/manual/pthread_join.html 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 Val

turns out that they are no longer able to create new threads, despite having plenty of free memory and http://www.scsc.no/blog/2007/11-15-thread-creation-using-pthreadcreate-on-leopard.html only a handful of other threads running. The culprit is a change in behaviour when calling pthread_create on Tiger vs. calling it on Leopard. On Tiger, it is possible https://m.ibm.com/https/publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/handheld/BOOKS/EDCLB100/4.1.35?DT=20010112090102&wirelessshow=2 to create millions of threads without worrying about cleaning up after them. This has changed in Leopard: Now, it is only possible to create 11002 (I wonder were pthread join that magical constant came from) threads before additional new threads can not be created. The following snippet of code demonstrates the issue: // A thread that just exits void* mythread(void *arg) { return 0; } int main (int argc, char *argv[]) { int err, count; pthread_t thread; while (1) { err = pthread_create(&thread, 0, mythread, 0); if (err pthread join error != 0) { printf("Count: %d Error: %d '%s'\n", count, err, strerror(err)); sleep(5); } count++; if (count % 1000 == 0) printf("Count: %d\n", count); } return 0; } To compile the code, put it in a file called pthread-test.c and compile it by running gcc -Wall -o pthread-test pthread-test.c from a terminal. Then run the binary by typing ./pthread-test . After a few seconds, the program will give the following output: [ fourlights ] ./pthread-test ... Count: 10000 Count: 11000 Count: 11002 Error: 35 'Resource temporarily unavailable' The fix to this particular problem is to always explicitly detach threads that you do not intend to pthread_join() with later on. That is, once the thread has been created, it should be detached as follows: err = pthread_create(&thread, 0, mythread, 0); if (err != 0) { printf("Count: %d Error: %d '%s'\n", count, err, strerror(err)); sleep(5); } pthread_detach(thread); With this fix, the example no longer fails after the aforementioned 11002 threads. Comments Please enable JavaScript to view the comments by Disqus.

Comments by Disqu

(void *arg), void *arg); GeneralDescription Creates a new thread within a process, with attributes defined by the thread attribute object, attr, that is created by pthread_attr_init(). If attr is NULL, the default attributes are used. See "pthread_attr_init()-- Initialize a Thread Attribute Object" in topic 4.1.17 for a description of the thread attributes and their defaults. If the attributes specified by attr are changed later, the thread's attributes are not affected. pthread_t is the data type used to uniquely identify a thread. It is returned by pthread_create() and used by the application in function calls that require a thread identifier. The thread is created running start_routine, with arg as the only argument. If pthread_create() completes successfully, thread will contain the ID of the created thread. If it fails, no new thread is created, and the contents of the location referenced by thread are undefined. System default for the thread limit in a process is set by MAXTHREADS in the BPXPRMxx parmlib member. The maximum number of threads is dependent upon the size of the private area below 16M. pthread_create() inspects this address space prior to creating a new thread. A realistic limit is 200 to 400 threads. SpecialBehaviorforC++ Because C and C++ linkage conventions are incompatible, pthread_create() cannot receive a C++ function pointer as the start routine function pointer If you attempt to pass a C++ function pointer to pthread_create(), the compiler will flag it as an error. You can pass a C or C++ function to pthread_create() by declaring it as extern "C". The started thread provides a boundary with respect to the scope of try-throw-catch processing. A throw done in the start routine or a function called by the start routine causes stack unwinding up to and including the start routine (or until caught). The stack unwinding will not go beyond the start routine back into the thread creator. If the exception is not caught, terminate() is called. The exception stack (for try-throw-catch) are thread-based. The throw of a condition, or re-throw of a condition by a thread does not affect exception processing on another thread, unless the condition is not caught. ReturnedValue If successful, pthread_create() returns the value 0.

 

Related content

error return code from pthread_join is 3

Error Return Code From Pthread join Is table id toc tbody tr td div id toctitle Contents div ul li a href What Does Pthread Join Do a li li a href Pthread join Multiple Threads Example a li li a href Pthread join Arguments a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you relatedl 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