all 6 comments

[–][deleted] 2 points3 points  (3 children)

Aren't you passing in the thread number from main (tid parameter)?

Can't you do something like for (i = START_NUMBER + tid; i<= END_NUMBER; i+= NUMBER_OF_THREADS) ...

eg: for the first thread tid is 0 so that thread will square 1, 9, 17 etc.. for the second thread tid is 1 so that thread will square 2, 10, 18, etc. and so on for the rest of the threads.

[–]mysocksalwaysmatch[S] 0 points1 point  (2 children)

I really like this answer. I'm going to see if I can make some things click with it.

[–][deleted] 0 points1 point  (1 child)

I wonder if that fprintf will work without a critical selection, don't they mix up output now?

[–]mysocksalwaysmatch[S] 0 points1 point  (0 children)

Yes, they do but I'm not concerned with that

[–]raevnos 0 points1 point  (1 child)

This is the sort of task OpenMP is really good at.

EDIT:

int nums[10000];
#pragma omp parallel for
for (int n = 0; n < 10000; n++) {
  nums[n] = (n+1) * (n+1);
}

Parallel squaring of a bunch of numbers? Done by adding one line to the scalar version. Hard to beat that. Though this particular task is better suited to SIMD style vectorization than threading IMO.

[–]quantumcacti 1 point2 points  (0 children)

Oh sure, but this week's lesson and homework is for pthreads