• POSIX example for pthread_join()

    From Spiros Bousbouras@21:1/5 to All on Fri Jul 14 11:05:52 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Spiros Bousbouras on Fri Jul 14 15:23:00 2023
    Spiros Bousbouras <[email protected]> writes: >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.


    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);

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Kettlewell@21:1/5 to Scott Lurndal on Fri Jul 14 17:25:50 2023
    [email protected] (Scott Lurndal) writes:
    Spiros Bousbouras <[email protected]> writes:
    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.

    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/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)