[英]pthread_mutex_errorcheck_np initialization error
Below code gives me compilation error: 下面的代码给我编译错误:
#include<pthread.h>
#include<stdio.h>
pthread_mutex_t mutex= PTHREAD_MUTEX_ERRORCHECK_NP;
int i=0;
void *func(void *a)
{
pthread_mutex_lock(&mutex);
i++;
printf("The value of thread is: %d\n",i);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main()
{
pthread_t id1,id2;
if(pthread_create(&id1,NULL,func,NULL)<0)
perror("pthread_create error\n");
pthread_join(id1,NULL);
pthread_join(id2,NULL);
}
The error is: 错误是:
error: invalid initializer
pthread_mutex_t mutex= PTHREAD_MUTEX_ERRORCHECK_NP;
related content of my pthread.h is: 我的pthread.h的相关内容是:
enum
{
PTHREAD_MUTEX_TIMED_NP,
PTHREAD_MUTEX_RECURSIVE_NP,
PTHREAD_MUTEX_ERRORCHECK_NP,
PTHREAD_MUTEX_ADAPTIVE_NP
#if defined __USE_UNIX98 || defined __USE_XOPEN2K8
,
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
#endif
#ifdef __USE_GNU
/* For compatibility. */
, PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
#endif
};
Why this compilation error is occuring? 为什么会出现此编译错误? I am not able to get any definite answer googling it.
我无法在Google上找到任何明确的答案。 I am using
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
我正在使用
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
The command I used for compilation : gcc prog.c -lpthread
我用于编译的命令:
gcc prog.c -lpthread
You need to use PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
when initializing the mutex statically with '=' (note the extra _INITIALIZER
). 用'='静态初始化互斥锁时,需要使用
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
(请注意额外的_INITIALIZER
)。 PTHREAD_MUTEX_ERRORCHECK_NP
is for when you're initializing it at runtime using pthread_mutex_init(3)
. PTHREAD_MUTEX_ERRORCHECK_NP
适用于在运行时使用pthread_mutex_init(3)
对其进行初始化的情况。
It is better to use -pthread
than -lpthread
by the way. 顺便说一句,使用
-pthread
比-lpthread
更好。 It sets preprocessor flags to make some functions reentrant for example. 例如,它设置预处理器标志以使某些函数可重入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.