多线程未定义的引用
#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