title: Passing struct to _beginThreadEx() function produces unintended output
body: <p>I'm trying to learn the "proper" way of passing multiple parameters to a _beginThreadEx function.  Could use some help.</p>
  
  <p>In function myThread.</p>
  
  <p>I keep getting either a 1, or 3, or 5 appended to the output of *x->value</p>
  
  <p>Also... if I place</p>
  
  <pre><code>cout &lt;&lt; "die: " &lt;&lt; random_num &lt;&lt; endl;
  </code></pre>
  
  <p>before </p>
  
  <pre><code>hThread[0] = (HANDLE)_beginthreadex(NULL, 0, myThread, &amp;myThreadData, 0, NULL);
  </code></pre>
  
  <p>sometimes there is no Hello World outputted, so instead I place it after the above line.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;process.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;time.h&gt;
  #include &lt;string&gt;
  #include &lt;windows.h&gt;
  
  #define PHI 0x9e3779b9
  
  static uint32_t Q[4096], c = 362436;
  
  
  using namespace std;
  
  class cmwc
  {
      public:
  
      void init_rand(uint32_t x)
      {
              int i;
  
              Q[0] = x;
              Q[1] = x + PHI;
              Q[2] = x + PHI + PHI;
  
              for (i = 3; i &lt; 4096; i++)
                      Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
      }
  
      uint32_t rand_cmwc(void)
      {
              uint64_t t, a = 18782LL;
              static uint32_t i = 4095;
              uint32_t x, r = 0xfffffffe;
              i = (i + 1) &amp; 4095;
              t = a * Q[i] + c;
              c = (t &gt;&gt; 32);
              x = t + c;
              if (x &lt; c) {
                      x++;
                      c++;
              }
              return (Q[i] = r - x);
      }
  
  };
  
  struct myThreadStructure
  {
      int *value;
      //~myThreadStructure() {delete[] string;}
  };
  
  unsigned __stdcall myThread(void *data)
  {
      //C:\dev\default threads\_threads.cpp|6|error: invalid conversion from 'int*' to 'int' [-fpermissive]|
  
      //works?
      //int *x = static_cast&lt;int*&gt;(data);
  
      myThreadStructure *x = static_cast&lt;myThreadStructure*&gt;(data);
  
      //int *x = (int*)data;
  
      std::cout &lt;&lt; "Hello World! " &lt;&lt; *x-&gt;value &lt;&lt; endl;
      _endthreadex(0);
  }
  
  int main()
  {
      cmwc rng1;
  
      myThreadStructure myThreadData;
  
      rng1.init_rand(time(NULL));
  
      uint32_t random_num = 1 + rng1.rand_cmwc()%6;
  
      //int r = rng1.rand_cmwc();
  
  
  
      HANDLE hThread[4];
  
      int x = 10;
  
      myThreadData.value = &amp;x;
  
      //works
      //hThread[1] = (HANDLE)_beginthreadex(NULL, 0, myThread, &amp;x, 0, NULL);
  
      hThread[0] = (HANDLE)_beginthreadex(NULL, 0, myThread, &amp;myThreadData, 0, NULL);
  
      WaitForMultipleObjects(0, hThread, TRUE, INFINITE);
  
      //delete [] myThreadData;
  
      cout &lt;&lt; "die: " &lt;&lt; random_num &lt;&lt; endl;
  
  
      //while(true);
      return 0;
  }
  </code></pre>