title: understanding qthreads - sharing data between threads
body: <p>I'm starting to understanding the mechanism of using threads, but I guess I'm stuck,</p>
  
  <p>If I understood, I have to create my own class, release the <code>run()</code> method and then create the thread.</p>
  
  <p>the problem is that my thread has to read from the gui(main thread) some variables, and them using them, it will create some other variables, which the mainwindow will read and plot.</p>
  
  <p>the thing is that I’m receiving a bluetooth connection, which has to be always active in a thread, but the gui has to be plotting values read from this thread. </p>
  
  <p>this is the function which needs to be on a separated thread:</p>
  
  <pre><code>// Listen to the device for data
  void gui::listen_device()
  {
      unsigned char buf[10];
      unsigned char crcval;
      fd_set readmask;
      struct timeval tv;
  
      tv.tv_sec = 0;
      tv.tv_usec = 28000;
  
      memset (buf, 0, 10);
  
      int v = 0, v1 = 0, v2 = 0;
  
      while(1)
      {
          int i;
          FD_ZERO (&amp;readmask);
          FD_SET (sock, &amp;readmask);
          if (select (255, &amp;readmask, NULL, NULL, &amp;tv) &gt; 0)
          {
              if (FD_ISSET (sock, &amp;readmask))
              {
                  int numb;
                  numb  = 0;
  
                  numb = recv (sock, buf, 10, MSG_WAITALL);
  
                  crcval = BP_CRC8 (buf, 9);
  
                  // 8 bits
                  if (ui-&gt;comboBox-&gt;currentIndex() == 0)
                  {
                      if (crcval == buf[9])
                      {
                          s++;
                          // Print of counter
                          printf ("%d -&gt;", buf[0]);
                          fprintf (data, "%d,", buf[0]);
  
                          for (int i = 1; i &lt; 9; i++)
                          {
                              v = buf[i];
                              printf ("%d,", v);
                              fprintf (data, "%d,", v);
                          }
  
                          printf ("\n");
                          fprintf (data, "\n");
  
                          //fprintf(data, "s: %d, f: %d\n", s,f);
                      }
  
                      else
                      {
                          f++;
                      }
  
                  }
  
                  // 12 bits
                  else if (ui-&gt;comboBox-&gt;currentIndex() == 1)
                  {
                      if (numb == 14)
                      {
                          // Print of counter
                          printf ("%d,", buf[0]);
                          fprintf (data, "%d,", buf[0]);
                          for (i = 1; i &lt; numb - 1; i += 3)
                          {
                              v1 = buf[i] | ((buf[i + 1] &amp; 0x0F) &lt;&lt; 8);
                              v2 = buf[i + 2];
                              v2 = (v2 &lt;&lt; 4) | ((buf[i + 1] &amp; 0xf0) &gt;&gt; 4);
                              printf ("%d,%d,", v1, v2);
                              fprintf (data, "%d,%d,", v1, v2);
                          }
  
                          printf ("\n");
                          fprintf (data, "\n");
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>and some of this variables read here are global, as <code>sock, data, and the ui-&gt;combobox</code>.</p>
  
  <p>I want the <code>buf</code>to be shared with the mainwindow.</p>
  
  <p>Any advice?</p>
  
  <p>UPDATE:</p>
  
  <p>why is this wrong?</p>
  
  <pre><code>void QMyThread::run()
  {
      listen_device();
  }
  </code></pre>