title: casting to void* to pass objects to pthread in c++
body: <p>I'm a little confused about how to pass an object to the pthread_create function. I've found a lot of piecemeal information concerning casting to void*, passing arguments to pthread_create, etc., but nothing that ties it all together. I just want to make sure I've tied it all together and am not doing anything stupid. Let's say I have the following thread class:
  <strong>Edit:</strong> fixed mis-matched <code>static_cast</code>.</p>
  
  <pre><code>class ProducerThread {
      pthread_t thread;
      pthread_attr_t thread_attr;
      ProducerThread(const ProducerThread&amp; x);
      ProducerThread&amp; operator= (const ProducerThread&amp; x);
      virtual void *thread_routine(void *arg) {
          ProtectedBuffer&lt;int&gt; *buffer = static_cast&lt;ProtectedBuffer&lt;int&gt; *&gt; arg;
          int randomdata;
  
          while(1) {
              randomdata = RandomDataGen();
              buffer-&gt;push_back(randomdata);
          }
  
          pthread_exit();
      }
  public:
      ProtectedBuffer&lt;int&gt; buffer;
  
      ProducerThread() {
          int err_chk;
  
          pthread_attr_init(&amp;thread_attr);
          pthread_attr_setdetachstate(&amp;thread_attr,PTHREAD_CREATE_DETACHED);
  
          err_chk = pthread_create(&amp;thread, &amp;thread_attr, thread_routine, static_cast&lt;void *&gt; arg);
          if (err_chk != 0) {
              throw ThreadException(err_chk);
          }
      }
      ~ProducerThread() {
          pthread_cancel(&amp;thread);
          pthread_attr_destroy(&amp;thread_attr);
      }
  }
  </code></pre>
  
  <p>To clarify, the data in the <code>ProtectedBuffer</code> class can only be accessed with methods like <code>ProtectedBuffer::push_back(int arg)</code>, which use mutexes to protect the actual data. </p>
  
  <p>My main question is: am I using <code>static_cast</code> correctly? And my secondary question is do I need that first line in <code>virtual void *thread_routine(void *arg)</code> where I copy the passed void pointer to a pointer to <code>ProtectedBuffer</code>?</p>
  
  <p>Also, if I've done anything else that might cause problems, I'd appreciate hearing it.</p>