tags:

views:

40

answers:

1
#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??

+6  A: 

You need to link with the Pthreads library:

gcc -lpthread za.c
James McNellis
i try it,but is not excuted
@lilyrose07: What do you mean "is not executed?" What error are you receiving?
James McNellis