我的代码是这样的
starts
是一个数组DWORD32
threads
是一个数组HANDLE
void initThreads(HANDLE* threads, int size)
{
DWORD32* starts = (DWORD32*)malloc(sizeof(DWORD32) * size);
for (int i = 0; i < size; ++i)
{
starts[i] = num_steps / numThreads * i;
}
for (int i = 0; i < size; ++i)
{
DWORD32* para = starts + i;
printf("create %ld\n", *para);
threads[i] = CreateThread(NULL, 0, portionCal, (void*)para, 0, NULL);
}
free(starts);
}
DWORD WINAPI portionCal(LPVOID pArg)
{
double x, portionSum = 0.0;
DWORD32 start = *(DWORD32*)pArg;
printf("start at %d\n", start);
}
但结果是
create 0
create 25000000
start at 0
create 50000000
create 75000000
start at 50000000
start at -17891602
start at 25000000
为什么结果看起来像这样?