title: pthread in a class
body: <p>Hey everyone, considering the following code (compiled with <code>g++ -lpthread thread_test.cpp</code>) how can I know what number thread I am in from inside "thread_function"? And let me know if you have any other suggestions.</p>
  
  <p>Thanks!</p>
  
  <p>thread_test.cpp:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  
  class A { 
     public:
        A();
        void run();
  
     private:
        static void* thread_function( void *ptr );
        pthread_t m_thread1, m_thread2;
  
        static int m_global;
  };
  
  int A::m_global = 0;
  
  A::A() {
     int ret1 = pthread_create( &amp;m_thread1, NULL, &amp;A::thread_function, this );
     int ret2 = pthread_create( &amp;m_thread2, NULL, &amp;A::thread_function, this );
  }
  
  void A::run() {
     while ( 1 ) { 
        printf( "parent incrementing...\n" );
        m_global++;
        sleep( 2 );
     }   
  }
  
  void* A::thread_function( void *ptr ) { 
     printf( "I'm thread ?\n" );
  
     while ( 1 ) { 
        printf("thread global: %d\n", m_global );
        sleep( 1 );
     }   
  }
  
  int main() {
     A a;
     a.run();
  
     return 0;
  }
  </code></pre>