I have this code and I am not getting the expected results… whats wrong?
typedef struct {
int data1;
int data2;
}t;
void foo(int a, int b) {
Handle handle;
t arg;
arg.data1 = a;
arg.data2 = b;
handle = (HANDLE) _beginthread( myFunc, 0, (void*) &arg);
}
void myFunc(void *param) {
t *args = (t*) param;
int x = args->data1;
int y = args->data2;
printf("x=%d, y=%d\n", x, y);
}
argis a local variable defined infoo– it would be destroyed as soon as that function ends, butmyFuncwhich is running in another thread would still be trying to access it. You should allocateargon the heap and destroy it in the thread after you are done.Also note that
HANDLEshould be all caps.