I revised a code i found on the net. It doesn't print/output "0 1 2 3" when running it?. any potential bad errors?.
#include <iostream>
#include <windows.h>
class Thread
{
public:
Thread();
static void * EntryPoint(void*);
int Start();
int Run(int arg);
void Execute(int);
int Arg() const {return Arg_;}
void Arg(int a){Arg_ = a;}
private:
int Arg_;
};
Thread::Thread() {}
void * Thread::EntryPoint(void * pthis)
{
Thread * pt = (Thread*)pthis;
pt->Run( pt->Arg() );
return 0;
}
int Thread::Start()
{
HANDLE h[4];
for (int i = 0; i < 4; i++)
{
DWORD dwArg = (DWORD) i;
Arg(dwArg);
h[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)EntryPoint, &dwArg, 0, NULL);
}
WaitForMultipleObjects(4, h, TRUE, INFINITE);
return 0;
}
int Thread::Run(int arg)
{
Execute( arg );
return 0;
}
void Thread::Execute(int a)
{
printf("%d\n", a);
}
int main()
{
Thread pt;
pt.Start();
return 0;
}