Re: pthread_create creating 2 threads on first call

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Nguyen,
If you look at the ps output, you'll see that your app's main thread owns the first thread created, and all the subsequent threads belong not to your app, but to that first thread created. That first thread is the thread manager thread created by the LinuxThreads library. It exists to control the other threads and enforce some semblance of POSIX compatibility. For example, the POSIX spec says that an unhandled signal sent to any thread in the app whose default action is to terminate tha application, should terminate all the threads in the app. Now remember that threads in LinuxThreads are actual processes, so an unhandled signal whose default action is to terminate the app would terminate only that particular thread, which is not POSIX compliant. So the thread manager thread is there to catch the signal and kill all the other threads, like POSIX says it should. Also, if these threads did not belong to a thread manager, they would be inherited by the init process everytime that the app's main thread finished, again because these "threads" are full blown processes that can exist outside and beyond it's parent; that all the child threads die when the parent thread dies is not normally the behaviour of Linux. That's something that LinuxThreads causes them to do in order to behave more like the way POSIX proscribes. I did some experimentation with the clone() system call, and sure enough, if the main app dumped core or something, I was left with all my threads running happily, but now owned by pid 1, the init process.
HTH,
Don
Nguyen, NguyenX D writes:
Hello folks,
I have a simple demo program that call pthread_create
5 times with identical parameters. On the FIRST call to pthread_create, two threads are created. One of these thread is what I am expecting. On subsequent call to pthread_create, only one thread is created as expected.
Does anyone happen to know why on the FIRST call to
pthread_create, two threads are created? What does the
extra one do? Many thanks!
- Nguyen
ps: I am compiling with the -pthread switch.
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux