title: Correct pthread_t initialization and handling
body: <p>I understand that <code>pthread_t</code> should be treated as an opaque value, however I don't know how to initialize it when used as a class member, and how can I check for its validity.</p>
  
  <p>Consider this code sample:</p>
  
  <pre><code>class MyClass
  {
  public:
      pthread_t thread;
  
      MyClass()
          : thread(0) // Wrong because pthread_t should be an opaque value,
                      // so how should I initialize it?
      {}
  
      ~MyClass()        
      {
          if( thread ) // Wrong, but how can I verify if it is valid?
              pthread_join(thread, NULL);
      }
  };
  </code></pre>
  
  <p>I also understand that if <code>pthread_create()</code> fails, the <code>pthread_t</code> value may be inconsistent. So I should only rely on the return value from <code>pthread_create()</code>. But this means that I should keep this returned value along with <code>pthread_t</code> and use it to check thread validity? And in this case, how should I initialize in the class constructor this value?</p>
  
  <pre><code>class MyClass
  {
  public:
      pthread_t thread;
      int threadValid;
  
      MyClass()
          : thread(0), // Wrong because pthread_t should be an opaque value,
                       // so how should I initialize it?
          , threadValid(1) // pthread_create return zero in case of success,
                           // so I can initialize this to any nonzero value?
      {}
  
      ~MyClass()        
      {
          if( threadValid == 0 ) // Nonzero means thread invalid.
                                 // Is this the correct approach?
          {
              pthread_join(thread, NULL);
              threadValid = 1;
          }
      }
  };
  </code></pre>
  
  <p>I have a Windows API background, and there a thread has its <code>HANDLE</code> value, which may be initialized safely to <code>NULL</code>, and can be checked against <code>NULL</code>, and if <code>CreateThread()</code> fails, it just consistently returns <code>NULL</code>. There's no way, with pthreads, to keep this neat and simple approach?</p>