Crash on WaitForSingleObject(threadHandle) when using intel compiler

Crash on WaitForSingleObject(threadHandle) when using intel compiler

I coded a multi-threading program that function very well when using microsoft c++ compiler.
But it crashes when changing to intel c++ compiler.

Handle hdl[5];
int idx=0;
The idx and hdl[5] are inited to 0 when the program starts.
Then the following function is called repeatly.

void function()
{
hdl[idx] = (HANDLE)_beginthreadex(0, 0, bthrd_dpcstub, pcur, 0, &thrdID);
printf("curr created handle %10x\\n", hdl[idx]);
idx++;
if(idx==5)
idx=0;
if(hdl[idx]!=0)
{printf("pre wait next handle %10x\\n", hdl[idx]);
WaitForSingleObject(hdl[idx], INFINITE);printf("aft wait next handle\\n");
CloseHandle(hdl[idx]);printf("aft close handle\\n");
hdl[idx] = 0;
}
}
It creates a new work thread and store it's handle to hdl[idx], then increment the idx to next idx.
then if the next hdl[idx] is with a working thread, wait for the end of the working thread and set the handle to 0.

The program runs fine with microsoft c++ compiler, but if I change to intel c++ compiler,
the results are following:
The handle of the created thread are as follows: 0x784, 0x788, 0x780, 0x77c, 0x778, 0x784
the next handle is as follows: 0, 0, 0, 0, 0x784, 0x788
It is all correct, but I found the program crashes on WaitForSingleObject(0x788, INFINITE), the created thread when the void function() is called in the second time, why? Is there some incompatibility between the two compiler?

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.