pthread_mutex_t 的类型是什么?

What is the type of pthread_mutex_t?

发件人:https://www.sourceware.org/pthreads-win32/manual/pthread_mutex_init.html

Variables of type pthread_mutex_t can also be initialized statically,

那么,pthread_mutex_t的类型是什么?

那个就是那个类型。下面的实现通常是一个结构,如果你真的关心你正在使用的库的具体实现,你可以查看头文件,但这些细节对于使用它并不重要,你只关心 pthread_mutex_t类型。

pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_t 是一种类型,所以它本身没有类型。如果您对这种类型的别名感到好奇,在我的机器上我有:

struct _opaque_pthread_mutex_t {
  long __sig;
  char __opaque[__PTHREAD_MUTEX_SIZE__];
};

然后

typedef struct _opaque_pthread_mutex_t __darwin_pthread_mutex_t;

最后:

typedef __darwin_pthread_mutex_t pthread_mutex_t;

从 pthreadtypes.h 开始,在我的 Linux 发行版中,它的定义非常清楚,作为联合的 typedef,定义如下:

/* Data structures for mutex handling.  The structure of the attribute
   type is not exposed on purpose.  */
typedef union
{
  struct __pthread_mutex_s
  {
    int __lock;
    unsigned int __count;
    int __owner;
    /* KIND must stay at this position in the structure to maintain
       binary compatibility.  */
    int __kind;
    unsigned int __nusers;
    __extension__ union
    {
      int __spins;
      __pthread_slist_t __list;
    };
  } __data;
  char __size[__SIZEOF_PTHREAD_MUTEX_T];
  long int __align;
} pthread_mutex_t;

你会希望将它用作他们定义的类型,pthread_mutex_t 当然 - 因为这种类型会因 OS / 分布 / 等而异。