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