compile error:c language in telnet(linux)
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>
int count=0;
void *thread_function(void *arg)
{
while(count<10)
{
if(count%2==1)
{
count++;
}
else
{sleep(1);}
}
}
int main(int argc,int *argv)
{
int res;
pthread_t a_thread[2];
void *thread_result;
int n;
while(count<10)
{
if(count%2==0)
{printf("%d",count);
count++;
}
else{sleep(1);}
}
for(n=0;n<2;n++)
{
pthread_create(&(a_thread[n]),NULL,thread_function,NULL);
}
while(count==9)
{pthread_join(a_thread[0],&thread_result);
}
while(count==10)
{
pthread_join(a_thread[1],&thread_result);
}
printf("%d",count);
return 0;
}
in telnet,linux
i write gcc za.c
error list: undefined reference to pthread_create,pthread_join in function 'main'
//开发者_开发百科why??
You need to link with the Pthreads library:
gcc -lpthread za.c
精彩评论