I am having problems with passing arguments to new threads, I have the following
doRowOffset[0] = 0;
for(threads = 1; threads < NUMTHREADS; threads++){
doRowOffset[threads] = threads;
printf("Do row: %d\n", doRowOffset[threads]);
pthread_create(&threadHandler[threads], NULL, multiplyRows, (void *) &doRowOffset[threads]);
}
and
void * multiplyRows(void * dealWithRows){
int offset = *((int *) dealWithRows);
printf("%d\n", *((int *) dealWithRows));
printf("Offset: %d\n", offset);
printf("Size: %d\n", partitionSize/NUMTHREADS);
printf("Calculated Offset: %d\n", offset*partitionSize/NUMTHREADS);
...
Now I expect to see the following output
Do row: 1
1
Offset: 1
Size: 2
Calculated offset: 2
However I keep getting garbage values or 0 for dealWithRows when I get into the thread am I doing something wrong with argument passing?
If you pass the address of a variable that is changing, you have to expect that it may change on you 🙂
What’s probably happening is that you’re setting
doRowOffsetto 1 (for example) then starting the thread. Before that thread kicks up and dereferences its pointer, you changedoRowOffsetfor the next thread.Then when the first thread finally dereferences the address, you’ve changed the underlying value.
There are two ways to handle this. The first is to synchronise the main thread and child thread so that the main one waits until the child had dereferenced the variable and stored the value locally. Only then does the main thread change the variable for the next thread it’s about to start. However, that sort of defeats the purpose of threads 🙂
The other is to cast the actual integer value (not the address) to a void pointer, which is passed to the thread function. This will mean the value will be copied to the stack of the thread function rather than the address, and the main thread will then not be able to affect it.
Something like:
in the main thread and:
in the child thread.