In the following code:
int main (int argc, const char * argv[]) {
// insert code here...
pthread_t t1, t2;
int sp1, sp2;
sp1 = pthread_create( &t1, NULL, getScalarProduct, NULL);
sp2 = pthread_create( &t2, NULL, getScalarProduct, NULL);
pthread_join( t1, NULL);
pthread_join( t2, NULL);
printf("Seperate scalars: %d %d\n", sp1, sp2);
finalScalarProd = sp1 + sp2;
printf("Result: %d\n", finalScalarProd);
return 0;
}
I’ve been unable to get anything back other than zero for the finalScalarProduct, and both sp1 and sp2 are zero also. I believe it’s something to do with the NULL argument being passed in pthread_join. I don’t really understand what this argument is for.
Any help appreciated!
That’s because pthread_create returns zero upon success.
The value is not the result of the main thread function, but the result of the thread creation (that might fail in some cases).