title: How to parallelize a function, that uses some variable
body: <p>I have a function, that generates random URL, and try to download the file.</p>
  
  <pre><code>void tryURL()
  {
      randURL.clear();
      for (unsigned short i = 0; i &lt; urlLength; i++)  {
      randURL = randURL + (alphabet[(int)((double)rand() / (RAND_MAX + 1) * alphabetLength)]);
      }
      wcout &lt;&lt; randURL &lt;&lt; endl;
      HRESULT getImg = URLDownloadToFile(NULL, LPCWSTR( (beginURL + randURL + endURL).c_str() ), LPCWSTR( (randURL + L"/" + endURL).c_str() ), 0, NULL);
      if (SUCCEEDED(getImg))
      {
      wcout &lt;&lt; L"Success" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>If I execute this function normally, It's work fine:</p>
  
  <pre><code>tryURL();
  0ybOAt
  tryURL();
  lTPKaR
  tryURL();
  Ivi05m
  ...
  </code></pre>
  
  <p>But, I need to run this function repeatedly times at that moment.
  I tried this:</p>
  
  <pre><code>thread threads[10];
  
  for (int i = 0; i &lt; 10; ++i) {
      threads[i] = thread(tryURL);
  }
  
  for (int i = 0; i &lt; 10; ++i) {
      threads[i].join();
  }
  </code></pre>
  
  <p>And it always returns me same value</p>
  
  <pre><code>0ybOAt0ybOAt
  0ybOAt
  
  0ybOAt0ybOAt
  
  0ybOAt
  0ybOAt0ybOAt
  0ybOAt
  0ybOAt
  </code></pre>
  
  <p>And even endl don't show's up sometimes.</p>
  
  <p>How can I fix it? I think, it broken because always using same variable randURL, but I don't know how to evade this. </p>