Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread_create and EAGAIN

I got an EAGAIN when trying to spawn a thread using pthread_create. However, from what I've checked, the threads seem to have been terminated properly.

What determines the OS to give EAGAIN when trying to create a thread using pthread_create? Would it be possible that unclosed sockets/file handles play a part in causing this EAGAIN (i.e they share the same resource space)?

And lastly, is there any tool to check resource usage, or any functions that can be used to see how many pthread objects are active at the time?

like image 969
kamziro Avatar asked Dec 26 '11 10:12

kamziro


2 Answers

Okay, found the answer. Even if pthread_exit or pthread_cancel is called, the parent process still need to call pthread_join to release the pthread ID, which will then become recyclable.

Putting a pthread_join(tid, NULL) in the end did the trick.

edit (was not waitpid, but rather pthread_join)

like image 134
kamziro Avatar answered Oct 05 '22 11:10

kamziro


As a practical matter EAGAIN is almost always related to running out of memory for the process. Often this has to do with the stack size allocated for the thread which you can adjust with pthread_attr_setstacksize(). But there are process limits to how many threads you can run. You can query the hard and soft limits with getrlimit() using RLIMIT_NPROC as the first parameter.

There are quite a few questions here dedicated to keeping track of threads, their number, whether they are dead or alive, etc. Simply put, the easiest way to keep track of them is to do it yourself through some mechanism you code, which can be as simple as incrementing and decrementing a global counter (protected by a mutex) or something more elaborate.

Open sockets or other file descriptors shouldn't cause pthread_create() to fail. If you reached the maximum for descriptors you would have already failed before creating the new thread and the new thread would have already have had to be successfully created to open more of them and thus could not have failed with EAGAIN.

like image 38
Duck Avatar answered Oct 05 '22 13:10

Duck