0

How do I use C within a program to assert that there are no threads (for example, by listing and counting them if necessary)?

I want to write a program that forks early-on before any threads are created but I want the application to abort if another developer changes a module in a way that a thread is created before the fork.

This is so we will find out about it and respond to ensure the thread creation code is moved so it runs after the fork.

codeshot
  • 1,183
  • 1
  • 9
  • 20
  • 1
    Marked as possible duplicate: http://stackoverflow.com/questions/3475750/posix-api-call-to-list-all-the-pthreads-running-in-a-process – Nidhoegger Sep 03 '15 at 08:05
  • A really simple way: Just `fork()`. It is safe to call, and the child will only have a single thread, regardless of how many threads the parent had. – EOF Sep 03 '15 at 09:48
  • 1
    @EOF: It is *not* safe to call, unless all you do afterwards in the child is call an `exec`-family function to replace the child process. This is because another thread might well be holding a crucial lock - for example an lock internal to the `malloc()` implementation - at the time of `fork()`. The concern is well-founded. – caf Sep 03 '15 at 10:44
  • @EOF: Also not safe for a second reason. The child process will be the one to do the threaded work. All threads must be created there or else some of the work will be done in the parent or fail to be done - and the parent is just a monitoring process. – codeshot Sep 03 '15 at 10:49

0 Answers0