当前位置: 技术问答>linux和unix
关于互斥量类型属性,在LINXU系统中为什么总是编译不通过?
来源: 互联网 发布时间:2016-08-17
本文导语: #include #include #include #include #include #include pthread_mutex_t mutex; int count; void *thread1(void *arg) { int err; printf("thread1 runn"); sleep(2); err=pthread_mutex_lock(&mutex); if(err!=0) strerror(err); err=pthread_mutex_lock(&mutex); ...
#include
#include
#include
#include
#include
#include
pthread_mutex_t mutex;
int count;
void *thread1(void *arg)
{ int err;
printf("thread1 runn");
sleep(2);
err=pthread_mutex_lock(&mutex);
if(err!=0)
strerror(err);
err=pthread_mutex_lock(&mutex);
if(err!=0)
strerror(err);
printf("thread1 overn");
return ((void *)0);
}
void *thread2(void *arg)
{
int err;
printf("thread2 runn");
err=pthread_mutex_lock(&mutex);
if(err!=0)
printf("thread2 pthread2_mutex failn");
count++;
pthread_mutex_unlock(&mutex);
printf("count=%d",count);
return ((void *)0);
}
int main(void)
{ int err;
pthread_t tid1,tid2;
pthread_mutexattr_t mutexattr;
err=pthread_mutexattr_init(&mutexattr);
if(err!=0)
strerror(err);
err=pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_PRIVATE);
if(err!=0)
strerror(err);
err=pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_NORMAL);//编程的时候总是说PTHREAD_MUTEX_NORMAL错误
if(err!=0)
strerror(err);
err=pthread_mutex_init(&mutex,&mutexattr);
if(err!=0)
printf("mutex chushihuaishibain");
err=pthread_create(&tid1,NULL,thread1,NULL);
if(err!=0)
printf("thread1 pthread_create failn");
err=pthread_create(&tid2,NULL,thread2,NULL);
if(err!=0)
printf("thread2 pthread_create failn");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_mutexattr_destroy(&mutexattr);
pthread_mutex_destroy(&mutex);
return 0;
}
这是我的写的个测试 互斥量类型属性的小程序 编译的时候总是说PTHREAD_MUTEX_NORMAL未定义过,我上面有包含头文件,不知道错在哪里,请高手指点
#include
#include
#include
#include
#include
pthread_mutex_t mutex;
int count;
void *thread1(void *arg)
{ int err;
printf("thread1 runn");
sleep(2);
err=pthread_mutex_lock(&mutex);
if(err!=0)
strerror(err);
err=pthread_mutex_lock(&mutex);
if(err!=0)
strerror(err);
printf("thread1 overn");
return ((void *)0);
}
void *thread2(void *arg)
{
int err;
printf("thread2 runn");
err=pthread_mutex_lock(&mutex);
if(err!=0)
printf("thread2 pthread2_mutex failn");
count++;
pthread_mutex_unlock(&mutex);
printf("count=%d",count);
return ((void *)0);
}
int main(void)
{ int err;
pthread_t tid1,tid2;
pthread_mutexattr_t mutexattr;
err=pthread_mutexattr_init(&mutexattr);
if(err!=0)
strerror(err);
err=pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_PRIVATE);
if(err!=0)
strerror(err);
err=pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_NORMAL);//编程的时候总是说PTHREAD_MUTEX_NORMAL错误
if(err!=0)
strerror(err);
err=pthread_mutex_init(&mutex,&mutexattr);
if(err!=0)
printf("mutex chushihuaishibain");
err=pthread_create(&tid1,NULL,thread1,NULL);
if(err!=0)
printf("thread1 pthread_create failn");
err=pthread_create(&tid2,NULL,thread2,NULL);
if(err!=0)
printf("thread2 pthread_create failn");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_mutexattr_destroy(&mutexattr);
pthread_mutex_destroy(&mutex);
return 0;
}
这是我的写的个测试 互斥量类型属性的小程序 编译的时候总是说PTHREAD_MUTEX_NORMAL未定义过,我上面有包含头文件,不知道错在哪里,请高手指点
|
两个办法
1. 编译命令行加个选项 -D_GNU_SOURCE
2. 用 PTHREAD_MUTEX_TIMED_NP 代替 PTHREAD_MUTEX_NORMAL
1. 编译命令行加个选项 -D_GNU_SOURCE
2. 用 PTHREAD_MUTEX_TIMED_NP 代替 PTHREAD_MUTEX_NORMAL
|
是-D__USE_UNIX98