Discussions of the Parallel Programming book
 help / color / mirror / Atom feed
* Section 4.2: wrong error reporting for pthread functions
@ 2018-07-14 12:59 Elad Lahav
  2018-07-14 23:33 ` Paul E. McKenney
  0 siblings, 1 reply; 13+ messages in thread
From: Elad Lahav @ 2018-07-14 12:59 UTC (permalink / raw)
  To: perfbook

Hello,

Throughout section 4.2 the following pattern is used for reporting
errors returned by various pthread functions:

if (pthread_func() == 0) {
    perror("pthread_func");
    exit(-1);
}

This is wrong, as pthread functions return the error number on failure
and do not set errno, as demonstrated by the following program, which
attempts to join with a detached thread:

#include <stdio.h>
#include <errno.h>
#include <pthread.h>

static void *
my_thread(void *arg)
{
    return NULL;
}

int
main()
{
    pthread_attr_t  attr;
    pthread_t       tid;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    errno = 0;

    if (pthread_create(&tid, &attr, my_thread, NULL) != 0) {
        perror("pthread_create");
        return 1;
    }

    if (pthread_join(tid, NULL) != 0) {
        perror("pthread_join");
        return 1;
    }

    return 0;
}

The result:
# ./pthread_error
pthread_join: Success

The correct way to report errors is to use strerror() on the returned code:

#include <string.h>
...
    int rc;
    rc = pthread_join(tid, NULL);
    if (rc != 0) {
        fprintf(stderr, "pthread_join: %s\n", strerror(rc));
        return 1;
    }

# ./pthread_error
pthread_join: Invalid argument

Also, using exit(-1) is likely to produce unexpected results for the
parent process. Error codes returned from processes are small
integers, with the range being platform-specific. This means that -1
will get truncated to some unspecified value. It is customary to use 1
to indicate a failure from a process (if no other specific code is
defined). stdlib.h provides EXIT_SUCCESS and EXIT_FAILURE, which may
be easier to read.

--Elad

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2018-07-18 19:21 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-14 12:59 Section 4.2: wrong error reporting for pthread functions Elad Lahav
2018-07-14 23:33 ` Paul E. McKenney
2018-07-16 15:42   ` Akira Yokosawa
2018-07-16 16:39     ` Paul E. McKenney
2018-07-17 15:43       ` Akira Yokosawa
2018-07-17 16:15         ` Paul E. McKenney
2018-07-18 12:15           ` Akira Yokosawa
2018-07-18 13:14             ` Paul E. McKenney
2018-07-18 13:42               ` Akira Yokosawa
2018-07-18 18:44                 ` Paul E. McKenney
     [not found]     ` <CAJbg=FW7vpepT_0wq+ffpQBHgKX+s1ruDb5Bs-m3FCPHKcPV3A@mail.gmail.com>
2018-07-16 23:51       ` Akira Yokosawa
2018-07-17 11:05         ` Elad Lahav
2018-07-17 16:27           ` Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox