user3768448
user3768448

Reputation: 15

PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP Error in QNX 7

I am porting code from Linux to QNX 7. PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is used in many places in my code. In Linux, this is defined in pthread.h. In QNX, I am unable to find where this is defined. Hence throwing error: "PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP' undeclared here (not in a function)".

How to fix this?

Upvotes: 2

Views: 4912

Answers (3)

Kevin Mathew
Kevin Mathew

Reputation: 1

You can try to configure with -D_XOPEN_SOURCE=700

CCFLAGS += -Werror \
    -D_POSIX_C_SOURCE=200809L \
    -Wall \
    -DUSE_VENDOR_EXT_PARAMS \
    -D_XOPEN_SOURCE=700

Upvotes: 0

Oleh Samoilenko
Oleh Samoilenko

Reputation: 89

/usr/include/pthread.h:
# ifdef __USE_GNU
#  define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP \
  { { 0, 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, __PTHREAD_SPINS, { 0, 0 } } }

Try to define __USE_GNU before including pthread:

#define __USE_GNU
#include <pthread.h>

Upvotes: 1

caf
caf

Reputation: 239341

Although recursive mutexes are standardised in POSIX, the standard doesn't specify a static initialiser for them (PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP is a glibc extension).

This means to be portable, you need to initialise your recursive mutexes manually with pthread_mutex_init():

int init_recursive_mutex(pthread_mutex_t *mutex)
{
    pthread_mutexattr_t attr;
    int r;

    r = pthread_mutexattr_init(&attr);
    if (r != 0)
        return r;

    r = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);

    if (r == 0)
        r = pthread_mutex_init(mutex, &attr);

    pthread_mutexattr_destroy(&attr);

    return r;
}

For the mutexes which you are statically initialising, you can instead initialise them with a function like the above called at the start of main() for each mutex.

Upvotes: 0

Related Questions