Reputation: 3
I need to create two threads, one of which will return even numbers and the other will return odd numbers. What am I doing wrong?
int _tmain(int argc, _TCHAR* argv[])
{
DWORD ID1 = 1, ID2 = 100;
DWORD arr[] = {ID1, ID2};
HANDLE h[1];
for (int i = 0; i < 2; ++i)
{
h[0] = CreateThread(NULL, 0, &f1, arr, 0, &arr[0]);
if (h[0] == NULL)
_tprintf(_T("%d"), GetLastError());
h[1] = CreateThread(NULL, 0, &f2, arr, 0, &arr[1]);
if (h[1] == NULL)
_tprintf(_T("%d"), GetLastError());
}
WaitForMultipleObjects(2, h, TRUE, INFINITE);
for (int i = 0; i < 2; ++i)
CloseHandle(h[i]);
return 0;
}
Upvotes: 0
Views: 137
Reputation: 307
Change this
HANDLE h[1];
for (int i = 0; i < 2; ++i)
{
h[0] = CreateThread(NULL, 0, &f1, arr, 0, &arr[0]);
if (h[0] == NULL)
_tprintf(_T("%d"), GetLastError());
h[1] = CreateThread(NULL, 0, &f2, arr, 0, &arr[1]);
if (h[1] == NULL)
_tprintf(_T("%d"), GetLastError());
}
To this
HANDLE h[2];
h[0] = CreateThread(NULL, 0, &f1, arr, 0, &arr[0]);
if (h[0] == NULL)
_tprintf(_T("%d"), GetLastError());
h[1] = CreateThread(NULL, 0, &f2, arr, 0, &arr[1]);
if (h[1] == NULL)
_tprintf(_T("%d"), GetLastError());
Upvotes: 3