title: Multitask-Process in C++ with Windows Form and Winsock
body: <p>I have to write a program which includes communication using TCP/IP and which is controllable through a Windows form. </p>
  
  <p>Therefore I have written a multithreaded program using <code>pthread.h</code>.  The one thread is for the Windows form, the other thread is for the Winsock stuff. </p>
  
  <pre><code>int main(int argc, char* argv[])
  {
      pthread_t t1, t2; // declare 2 threads.
      pthread_create(&amp;t1, NULL, RunTCPIP, NULL); // create a thread running TCP-Communication
      pthread_create(&amp;t2, NULL, RunForms, NULL); // create a thread running Windows Forms
      pthread_exit(NULL);
      return 0;
  }
  
  void * RunForms(void * argument)
  {
      //run GUI
      Application::EnableVisualStyles();
      Application::SetCompatibleTextRenderingDefault(false);
      Server_C_GUI::Mainform form;
      Application::Run(%form);
      return 0;
  }
  
  void * RunTCPIP(void * argument)
  {
      //Winsock-stuff....
      return 0;
  }
  </code></pre>
  
  <p>There are some variables which I have to use in both threads. And that's my problem. I have created <code>globals.h</code> where I have defined my global variables. </p>
  
  <pre><code>#include &lt;string&gt;
  using namespace std;
  
  #ifndef EXTERN
  #define EXTERN extern
  #endif
  
  //Variables for OperatingMode &amp; GUI
  EXTERN const string szServerResponse;
  EXTERN char cPlay;
  EXTERN char cBreak;
  EXTERN char cQuit;
  </code></pre>
  
  <p>I have included <code>globals.h</code> in the main.cpp - there it works. But when I try to include <code>globals.h</code> in <code>MyForm.h</code> - the header of my CLR Windows form - several failures arise. </p>
  
  <p>In <code>MyForm.h</code> I would like, for example, to write the following code:</p>
  
  <pre><code>private:
      System::Void btnQuit_Click(System::Object^  sender, System::EventArgs^  e)
      {
          cQuit = 0; //cQuit = global variable
      } 
  </code></pre>
  
  <p>How can I use global variables in a multithreaded program with Winsock and CLR Windows forms? What am I doing wrong?</p>