Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this strange definition for PTHREAD_CREATE_JOINABLE, PTHREAD_CREATE_DETACHED

Tags:

c

In pthread.h, one could find the following definition:

/* Detach state.  */
enum
{
  PTHREAD_CREATE_JOINABLE,
#define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
  PTHREAD_CREATE_DETACHED
#define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
};

Why this definition mixing enum and defines? How to interpret it? Besides, the #define is defining something to the same.

like image 674
user1284631 Avatar asked Mar 23 '23 05:03

user1284631


1 Answers

This is because PTHREAD_CREATE_JOINABLE and PTHREAD_CREATE_DETACHED need to be used in #ifdef statements (here is an example of source code where it is used in this way). The authors needed to define their names to their values, otherwise the preprocessor would not let you use the corresponding enum value.

like image 196
Sergey Kalinichenko Avatar answered Apr 05 '23 22:04

Sergey Kalinichenko