Search code examples
pthreadsposixmemory-leaks

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?


Solution

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