在linux中创建pthread_的未定义引用(c编程) #包括 #包括 #包括 扫描电镜e,n,s; INTA[10]; int标志=0; int-sizeb=10; 作废 { 指出; 如果(标志==0) { printf(“消费者正在等待\n”); } 其他的 { printf(“\n消费者已输入cs:\n”); 对于(out=0;out
在这种特定情况下,使用-pthread比使用-lpthread更可取。在这种特定情况下,使用-pthread比使用-lpthread更可取。您是如何编译它的?? 使用gcc程序编译\u name-lpthread您是如何编译它的??在linux中创建pthread_的未定义引用(c编程) #包括 #包括 #包括 扫描电镜e,n,s; INTA[10]; int标志=0; int-sizeb=10; 作废 { 指出; 如果(标志==0) { printf(“消费者正在等待\n”); } 其他的 { printf(“\n消费者已输入cs:\n”); 对于(out=0;out,c,linux,pthreads,C,Linux,Pthreads,在这种特定情况下,使用-pthread比使用-lpthread更可取。在这种特定情况下,使用-pthread比使用-lpthread更可取。您是如何编译它的?? 使用gcc程序编译\u name-lpthread您是如何编译它的?? 编译时在命令行中使用gcc程序_name-lpthread添加-lpthread,以在链接时搜索pthreads库。编译时在命令行中添加-lpthread,以在链接时搜索pthreads库 #include<stdio.h> #include<pt
编译时在命令行中使用gcc程序_name-lpthread添加
-lpthread
,以在链接时搜索pthreads库。编译时在命令行中添加-lpthread
,以在链接时搜索pthreads库
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t e,n,s;
int a[10];
int flag=0;
int sizeb=10;
void take()
{
int out;
if(flag==0)
{
printf("the consumer is waiting\n");
}
else
{
printf("\n the consumer has entered the cs:\n");
for(out=0;out<10;out++)
{
printf("\t%d",a[out]);
}
}
}
void consumer()
{
sem_wait(&n);
sem_wait(&s);
printf("\n Consumer Unit\n");
take();
if(flag==1)
{
printf("\n the consumer has consumed\n");
}
sem_post(&s);
sem_post(&e);
}
void append()
{
int in;
printf("\n the producer has entered the cs\n");
for(in=0;in<10;in++)
{
a[in]=in+1;
printf("\t%d",a[in]);
}
}
void producer()
{
sem_wait(&e);
sem_wait(&s);
printf("\n producer unit\n");
append();
printf("\n the producer has produced\n");
sem_post(&s);
sem_post(&n);
flag=1;
}
int main()
{
sem_init(&s,0,1);
sem_init(&n,0,1);
sem_init(&e,0,20);
pthread_t p1,p2,p3;
pthread_create(&p1,NULL,(void *)consumer,NULL);
pthread_join(p1,NULL);
pthread_create(&p2,NULL,(void *)producer,NULL);
pthread_join(p2,NULL);
pthread_create(&p3,NULL,(void *)consumer,NULL);
pthread_join(p3,NULL);
return 0;
}