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