LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Pthread Problem (https://www.linuxquestions.org/questions/programming-9/pthread-problem-437341/)

IBall 04-21-2006 01:21 AM

Pthread Problem
 
Hi,
The program below uses POSIX pthreads, and the child prints out 5 and the parent prints out 0. On Mac OS X, it compiles and runs correctly. On Linux I get the following error:
Code:

/tmp/ccCWW53M.o: In function `main':
q3.c:(.text+0x49): undefined reference to `pthread_create'
q3.c:(.text+0x5a): undefined reference to `pthread_join'
collect2: ld returned 1 exit status

Can anyone tell me what the problem is. I assume that it is a problem with pthread.h, but the problem is the same on Kubuntu, Fedora and Slackware systems that I have tried. pthread.h exists in all cases, and other programs (not using pthreads, but using other system calls) compile and work correctly.

Here is the program:
Code:

#include <pthread.h>
#include <stdio.h>

int value = 0;
void *runner(void *param);      /* The Thread*/

int main( int argc, char *argv[])
{
  int pid;
  pthread_t tid;
  pthread_attr_t attr;

  pid = fork();

  if (pid == 0)        /* Child Process */
  {
    pthread_attr_init(&attr);
    pthread_create(&tid, &attr, runner, NULL);
    pthread_join(tid,NULL);

    printf("CHILD: Value = %d\n",value);        /* Line C*/
  }
  else if (pid > 0)    /* Parent Process */
  {
    wait(NULL);
    printf("PARENT: value = %d\n",value);      /* LINE P*/
  }
}

void *runner(void *param)
{
  value = 5;
  pthread_exit(0);
}

Thanks in advance for any help
--Ian

scoban 04-21-2006 01:32 AM

compile with -lpthread option.

gcc x.c -lpthread

IBall 04-21-2006 02:05 AM

Excellent. Thankyou very much.

--Ian


All times are GMT -5. The time now is 03:30 PM.