Discussion:
POSIX example for pthread_join()
(too old to reply)
Spiros Bousbouras
2023-07-14 11:05:52 UTC
Permalink
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html :

((subarray *)arg)->ar[i]++;

This seems to me to be undefined behaviour since arr[] is not
initialised.
Scott Lurndal
2023-07-14 15:23:00 UTC
Permalink
Post by Spiros Bousbouras
((subarray *)arg)->ar[i]++;
This seems to me to be undefined behaviour since arr[] is not
initialised.
it's true that arr[] is not initialized. ar[] is, however.


int ar[1000000];
...
sb1.ar = &ar[0];
sb1.n = 500000;
(void) pthread_create(&th1, NULL, incer, &sb1);
Richard Kettlewell
2023-07-14 16:25:50 UTC
Permalink
Post by Scott Lurndal
Post by Spiros Bousbouras
((subarray *)arg)->ar[i]++;
This seems to me to be undefined behaviour since arr[] is not
initialised.
it's true that arr[] is not initialized. ar[] is, however.
int ar[1000000];
...
sb1.ar = &ar[0];
sb1.n = 500000;
(void) pthread_create(&th1, NULL, incer, &sb1);
There is no arr, but that’s just a typo.

I agree with Spiros. sb1.ar and sb2.ar are initialized; they point to
ar[0] and ar[500000] respectively. The contents of the ar array on
main’s stack are not initialized, so both threads will do an
uninitialized read on every iteration.

If the example was simpler it would be more likely to be correct l-)
--
https://www.greenend.org.uk/rjk/
Loading...