Thursday, 22 August 2013

Why is _GNU_SOURCE macro required for pthread_mutexattr_settype()?

Why is _GNU_SOURCE macro required for pthread_mutexattr_settype()?

I have written a multithread server program in C, which echoes back all
the data that a client sends.
Initially, I used poll() function in my program to detect POLLRDHUP event,
for that I defined _GNU_SOURCE macro (This event is defined here).
Later I updated my code & removed poll() function, however I forgot to
remove _GNU_SOURCE macro.
Now my code is finally complete (and a little long to post, more than 250
lines). Before removing macro I was compiling my program using:
gcc multi_thread_socket_v4.c -Wall -Werror -g -lpthread -o
multi_thread_socket
and it worked fine: No errors, no warnings
After I removed the macro definition, and compiled using same
command-line, the output of gcc was:
multi_thread_socket_v4.c: In function 'main':
multi_thread_socket_v4.c:194: warning: implicit declaration of function
'pthread_mutexattr_settype'
multi_thread_socket_v4.c:194: error: 'PTHREAD_MUTEX_ERRORCHECK' undeclared
(first use in this function)
multi_thread_socket_v4.c:194: error: (Each undeclared identifier is
reported only once
multi_thread_socket_v4.c:194: error: for each function it appears in.)
I have included all the required libraries as it worked fine initially.
I peeked into pthread.h at /usr/include/pthread.h and found out this:
/* Mutex types. */
enum
{
PTHREAD_MUTEX_TIMED_NP,
PTHREAD_MUTEX_RECURSIVE_NP,
PTHREAD_MUTEX_ERRORCHECK_NP,
PTHREAD_MUTEX_ADAPTIVE_NP
#ifdef __USE_UNIX98
,
PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP,
PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL

No comments:

Post a Comment