多线程未定义的引用

czy__412 2017-06-27 11:34:19
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>

int peas = 100;
int num;
int num1;
int num2;
int i;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;

void* thread1(void* arg)
{
//struct mutex_cond *mcond = (struct mutex_cond*)arg;
pthread_mutex_lock(&mutex);
for(i = 1;i < 100;i++)
{
if(i % 3 == 0)
{
pthread_cond_signal(&cond);
}

if(i % 4 == 0)
{
pthread_cond_signal(&cond1);
}
printf("[%d]这个是绿豆\n",i);
num++;
}
printf("绿豆个数为:%d\n",num);
pthread_mutex_unlock(&mutex);
}
void* thread2(void* arg)
{
//struct mutex_cond *mcond = (struct mutex_cond*)arg;
pthread_mutex_lock(&mutex);
for(i = 1;i < 100;i++)
{
if(i % 3 == 0)
{
pthread_cond_wait(&cond,&mutex);
}
num1++;
printf("[%d]这个是黄豆\n",i);
}
printf("黄豆个数为:%d\n",num1);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
void* thread3(void* arg)
{
//struct mutex_cond *mcond = (struct mutex_cond*)arg;
pthread_mutex_lock(&mutex);
for(i = 1;i < 100;i++)
{
if(i % 4 == 0)
{
pthread_cond_wait(&cond1,&mutex);
}
num2++;
printf("[%d]这个是黑豆\n",i);
}
printf("黑豆个数为:%d\n",num2);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main()
{
/*struct mutex_cond
{
pthread_mutex_t mutex;
pthread_cond_t cond;
} mcond;*/

pthread_t thid1;
pthread_t thid2;
pthread_t thid3;

pthread_mutex_init(&mutex,NULL);

pthread_cond_init(&cond,NULL);
pthread_cond_init(&cond1,NULL);


pthread_create(&thid1,NULL,thread1,NULL);
pthread_create(&thid2,NULL,thread2,NULL);
pthread_create(&thid3,NULL,thread3,NULL);

pthread_mutex_destory(&mutex);
pthread_cond_destory(&cond);
pthread_cond_destory(&cond1);

pthread_join(thid1,NULL);

return 0;
}


czy@czy-Aspire-V5-552G:~/作业/6.26程周游互斥$ gcc pear.c -lpthread
pear.c: In function ‘main’:
pear.c:93:2: warning: implicit declaration of function ‘pthread_mutex_destory’ [-Wimplicit-function-declaration]
pthread_mutex_destory(&mutex);
^
pear.c:94:2: warning: implicit declaration of function ‘pthread_cond_destory’ [-Wimplicit-function-declaration]
pthread_cond_destory(&cond);
^
/tmp/cceDaORg.o:在函数‘main’中:
pear.c:(.text+0x2ec):对‘pthread_mutex_destory’未定义的引用
pear.c:(.text+0x2fc):对‘pthread_cond_destory’未定义的引用
pear.c:(.text+0x30c):对‘pthread_cond_destory’未定义的引用
collect2: error: ld returned 1 exit status
...全文
331 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
建筑师 2017-06-27
  • 打赏
  • 举报
回复
有蹊跷, 慢慢找
czy__412 2017-06-27
  • 打赏
  • 举报
回复
谢啦。。。。。。。。
自信男孩 2017-06-27
  • 打赏
  • 举报
回复
pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    pthread_cond_destroy(&cond1);
单词拼写错误;注意下:destroy: ro还是or,

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧