title: Multithreading using pthread in C++ with shared variables
body: <p>I'm new to threading (and C/C++ for that matter), and I'm attempting to use multiple threads to access shared variables.</p>
  
  <p>In the main, I've created a variable char inputarray[100];</p>
  
  <p>Thread 1: This thread will be reading data from stdin in 2 byte bursts, and appending them to the inputarray. (input by feeding a file in)</p>
  
  <p>Thread 2: This thread will be reading data 1 byte at a time, performing a calculation, and putting its data into an output array.</p>
  
  <p>Thread 3: This thread will be outputting data from the output array in 2 byte bursts. (stdout)</p>
  
  <p>I've attempted the input part and got it working by passing a struct, but would like to do it without using a struct, but it has been giving me problems.</p>
  
  <p>If I can get input down, I'm sure I'll be able to use a similar strategy to complete output. Any help would be greatly appreciated.</p>
  
  <p>Below is a rough template for the input thread.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  
  using namespace std;
  
  void* input(void* arg) {
      char reading[3];
      fread(reading,1,2,stdin);
  
      //append to char inputarray[]..???
  }
  
  int main() {
      char inputarray[100];
      pthread_t t1;
      pthread_create(&amp;t1, NULL, &amp;input, &amp;inputarray);
      void *result;
      pthread_join(t1,&amp;result);
      return 0;
  }  
  </code></pre>