Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

POSIX example for pthread_join()

13 views
Skip to first unread message

Spiros Bousbouras

unread,
Jul 14, 2023, 7:06:02 AM7/14/23
to
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

unread,
Jul 14, 2023, 11:23:08 AM7/14/23
to
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

unread,
Jul 14, 2023, 12:25:54 PM7/14/23
to
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/
0 new messages