title: pthread - accessing multiple objects with a thread
body: <p>I'm trying to get my hands on multi threading and it's not working so far. I'm creating a program which allows serial communication with a device and it's working quite well without multi threading. Now I want to introduce threads, one thread to continuously send packets, one thread to receive and process packets and another thread for a GUI. </p>
  
  <p>The first two threads need access to four classes in total, but using <code>pthread_create()</code> I can only pass one argument. I then stumled upon a post here on stack overflow (<a href="https://stackoverflow.com/questions/1151582/pthread-function-from-a-class">pthread function from a class</a>) where Jeremy Friesner presents a very elegant way. I then figured that it's easiest to create a Core class which contains all the objects my threads need access to as well as all functions for the threads.So here's a sample from my class Core:</p>
  
  <pre><code>/** CORE.CPP **/
  
  #include "SerialConnection.h"   // Clas for creating a serial connection using termios
  #include "PacketGenerator.h"    // Allows to create packets to be transfered 
  #include &lt;pthread.h&gt;
  
  #define NUM_THREADS 4
  
  class Core{
  private:
    SerialConnection serial;    // One of the objects my threads need access to
    pthread_t threads[NUM_THREADS];
    pthread_t = _thread;
  
  public:
    Core();
    ~Core();
    void launch_threads();    // Supposed to launch all threads
    static void *thread_send(void *arg);    // See the linked post above 
    void thread_send_function();    // See the linked post above
  };
  
  Core::Core(){
    // Open serial connection
    serial.open_connection();
  }
  
  Core::~Core(){
    // Close serial connection
    serial.close_connection();
  }
  
  void Core::launch_threads(){
    pthread_create(&amp;threads[0], NULL, thread_send, this);
  
    cout &lt;&lt; "CORE: Killing threads" &lt;&lt; endl;
    pthread_exit(NULL);
  }
  
  void *Core::thread_send(void *arg){
    cout &lt;&lt; "THREAD_SEND launched" &lt;&lt; endl;
    ((Core *)arg)-&gt;thread_send_function(); 
    return NULL;
  }
  
  void Core::thread_send_function(){
    generator.create_hello_packet();
    generator.send_packet(serial);
    pthread_exit(NULL);
  }   
  </code></pre>
  
  <p>Problem is now that my serial object crashes with segmentation fault (that pointer stuff going on in <code>Core::thread_send(void *arg)</code> makes me suspicious. Even when it does not crash, no data is transmitted over the serial connection even though the program executed without any errors. Execution form main:</p>
  
  <pre><code>/** MAIN.CPP (extract) VARIANT 1 **/
  int main(){
    Core core;
    core.launch_threads();    // No data is transferred
  }
  </code></pre>
  
  <p>However, if I call the thread_send_function directly (the one the thread is supposed to execute), the data is transmitted over the serial connection flawlessly:</p>
  
  <pre><code>/** MAIN.CPP (extract) VARIANT 1 **/
  int main(){
    Core core;
    core.thread_send_function();    // Data transfer works
  }
  </code></pre>
  
  <p>Now I'm wondering what the proper way of dealing with this situation is. Instead of that trickery in Core.cpp, should I just create a struct holding pointers to the different classes I need and then pass that struct to the pthread_create() function? What is the best solution for this problem in general? </p>