Reputation: 2934
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?
Upvotes: 3
Views: 4262
Reputation: 882686
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 doRowOffset
to 1 (for example) then starting the thread. Before that thread kicks up and dereferences its pointer, you change doRowOffset
for 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:
pthread_create (&threadHandler[threads], NULL, multiplyRows,
(void *) doRowOffset); // <<-- Note, removed the '&'.
in the main thread and:
int offset = (int) dealWithRows;
in the child thread.
Upvotes: 2
Reputation: 96326
If doRowOffset
is an int
, you shouldn't see garbage, unless the thread creating function finishes and you allocated the variable on the stack and it gets overwritten.
But note that there is only one variable, and you are passing its address, so when the thread starts it could see an updated value.
Pass the actual value:
pthread_create(&threadHandler[threads], NULL, multiplyRows, (void *) threads);
void * multiplyRows(void * dealWithRows){
int offset = (int) dealWithRows);
}
Upvotes: 2