windowsmultithreadingcreatethread

Passing parameter fail by CreateThread?


My Code like this

starts is a array of DWORD32

threads is a array of 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);
}

But the result is

create 0
create 25000000
start at 0
create 50000000
create 75000000
start at 50000000
start at -17891602
start at 25000000

Why the result look like this?


Solution

  • We can't see the scope of starts but this can be guessed at from the failure. It is probably a local variable, long gone when the thread starts running. So you'll just read garbage. You'll need a stable pointer, get one from a global variable or malloc().

    After edit: don't call free() like that. It has to remain stable until all threads have completed using it. You could consider reference counting it with InterlockedDecrement().