title: Why does pthread_create() returns 0 but the thread never starts
body: <p>I am coming from a Java background and am comfortable using Java's synchronization for multi threaded programming. Recently, I started to dabble in C++ and have had to work on some multi-threaded code. Following is the smallest working example of the issue that I am facing.</p>
  
  <pre><code>class TxStatus {
      private:
          int numOperations;
          bool commitStatus;
          pthread_mutex_t statusLock;
  
      public:
          TxStatus() {
              this-&gt;commitStatus = false;
              this-&gt;numOperations = 0;
          }
          void addNewOperation() {
              pthread_mutex_lock(&amp;statusLock);
              numOperations++;
              pthread_mutex_unlock(&amp;statusLock);
          }
          void operationCompleted() {
              pthread_mutex_lock(&amp;statusLock);
              numOperations--;
              pthread_mutex_unlock(&amp;statusLock);
          }
          void commit() {
              this-&gt;commitStatus = true;
          }
  
  };
  
  class TxManager {
      private:
          unsigned long long globalFrontier;
          unsigned long long txId;
  
          pthread_mutex_t gfLock;
          pthread_mutex_t txIdLock;
  
          std::map&lt;unsigned long long, TxStatus&gt; txStatusMap;
  
      public:
          TxManager() {
              pthread_mutex_lock(&amp;gfLock);
              globalFrontier = 1;
              pthread_mutex_unlock(&amp;gfLock);
  
              pthread_mutex_lock(&amp;txIdLock);
              txId = 1;
              pthread_mutex_unlock(&amp;txIdLock);
          }
  
          unsigned long long beginNewTx() {
              pthread_mutex_lock(&amp;txIdLock);
              unsigned long long newId = txId;
              txId++;
              pthread_mutex_unlock(&amp;txIdLock);
              TxStatus statusObj;
              txStatusMap.insert(std::make_pair(newId,statusObj));
              return newId;
          }
  
          void addUnflushedOperation(unsigned long long txId) {
              txStatusMap[txId].addNewOperation();
          }
          void markOperationAsFlushed(unsigned long long txId) {
              txStatusMap[txId].operationCompleted();
          }
          void markCommitted(unsigned long long txId) {
              txStatusMap[txId].commit();
          }
  };
  
  
  void * thread( void *args){
  
      TxManager txManager;
      fprintf(stderr,"Inside thread");
      unsigned long long newTxId = txManager.beginNewTx();
  
      fprintf(stderr,"Tx Started: %d", newTxId );
      txManager.addUnflushedOperation(newTxId);
      pthread_exit(NULL);   
  
  }
  
  int main(){
      pthread_t tx_thread;
      fprintf(stderr,"Inside main");
      int ret = pthread_create(&amp;tx_thread, NULL, thread, NULL);
      if (ret != 0)
      {
          fprintf(stderr,"Error launching thread");
      }else{
          fprintf(stderr,"Thread launched successfully");
      }
  
  
      if (pthread_join(tx_thread, NULL) != 0)
      {
          fprintf(stderr,"Join pthread failed");
      }
  
      return 0;
  
  }
  </code></pre>
  
  <p>The thread is launched successfully but I never see one of the prints from the functions executed in the thread itself i.e thread(). If I remove the join call then the program just terminates after printing the statements inside the main method.</p>