HELP in a Thread code

Discussion in 'Programming/Scripts' started by full_adder, Aug 14, 2008.

  1. full_adder

    full_adder New Member

    Hi everyone

    I wrote the following code from an OS book. But when I compiled it the following msg had appeared:

    In function 'main': undefined reference to 'pthread_create'
    In function 'main': undefined reference to 'pthread_join'

    I don't understand what it means exactly!!



    Code:
    #include<pthread.h>
    #include<stdio.h>
    int sum;
    void* runner(void *param);
    int main(int argc, char *argv[]){
    	pthread_t tid;
    	pthread_attr_t attr;
    
    	if(argc !=2){
    		printf("usage: a.out <int value>\n");
    		return -1;
    	}
    	if( atoi(argv[1]) < 0 ){
    		printf("%d must be >= 0\n",atoi(argv[1]));
    		return -1;
    	}
    	pthread_attr_init(&attr);
    	pthread_create(&tid,&attr,(void *)runner,(void *)argv[1]);
    	pthread_join(tid,NULL);
    
    	printf("sum = %d\n",sum);
    }
    void* runner(void *param){
    	int i, upper=atoi(param);
    	sum=0;
    	for(i=1;i<=upper;i++)
    		sum +=i;
    	pthread_exit(0);
    }
     
  2. tempora

    tempora New Member

    solution

    compile this code in the following way

    gcc code.c -lpthread

    where code.c is assumed to be the filename of your c code.


    I have just registered to reply. i shall not be checking back. so, dont expect a reply. sorry for inconvenience.
     

Share This Page