#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <windows.h>
#include <process.h>
using namespace std;
HANDLE ghEvents;
struct DataStructure
{
int r[2];
int h;
};
unsigned __stdcall f2(void *p)
{
DataStructure *input = (DataStructure *)p;
int i = (int)input->h;
cout <<i<<endl;
}
int main()
{
HANDLE hThread[8];
DWORD i, dwEvent, dwThreadID;
unsigned threadID;
DataStructure input;
ghEvents = CreateEvent(NULL,FALSE,FALSE,NULL);
for (int i = 0; i < 8; i++)
{
input.h=i;
hThread[i] = (HANDLE)_beginthreadex( NULL, 0, f2, (void*)&input, 0, &threadID );
}
dwEvent = WaitForMultipleObjects(8,hThread,TRUE,INFINITE);
CloseHandle(ghEvents);
for (int i = 0; i < 8; i++)
{
CloseHandle( hThread[i] );
}
cin.get();
return 0;
}
The output is 77777777 instead of 12345678.
I know i have to pass the input by value and not reference but it keeps giving me an error message, what is the proper way to do it?
This is subsequent to my previous answer as is a better solution if the number of threads is known at compile time.
And you need to return a value: