title: pthreads: how to assert code is run in a single threaded context
body: <p>I am writing a C library which needs to fork() during initialization. Therefore, I want to assert() that the application code (which is outside of my control) calls my library initialization code from a single threaded context (to avoid the well known "<a href="http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them" rel="nofollow">threads and fork don't mix</a>" problem). Once my library has been initialized, it is thread safe (and expected that the application level code may create threads). I am only concerned with supporting pthreads.</p>
  
  <p>It seems impossible to count the number of threads in the current process space using pthreads. Indeed, even googletest only implements <a href="http://code.google.com/p/googletest/source/browse/trunk/src/gtest-port.cc#128" rel="nofollow">GetThreadCount()</a> on Mac OS and QNX.</p>
  
  <p>Given that I can't count the threads, is it somehow possible that I can instead assert a single threaded context?</p>
  
  <p>Clarification: If possible, I would like to avoid using "/proc" (non-portable), an additional library dependency (like libproc) and LD_PRELOAD-style pthread_create wrappers.</p>
  
  <p>Clarification #2: In my case using multiple processes is necessary as the workers in my library are relatively heavy weight (using webkit) and might crash. However, I want the original process to survive worker crashes.</p>