LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2006, 01:21 AM   #1
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 61
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
 
Old 04-21-2006, 01:32 AM   #2
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
compile with -lpthread option.

gcc x.c -lpthread
 
Old 04-21-2006, 02:05 AM   #3
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Original Poster
Rep: Reputation: 61
Excellent. Thankyou very much.

--Ian
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pthread problem with C++. Please Help allomeen Programming 20 12-16-2005 03:37 PM
pthread problem on redhat9 wizardjack Programming 4 02-02-2005 08:54 AM
pthread problem robnob Programming 16 11-12-2003 07:49 PM
pthread signal problem xinwang00 Programming 12 11-11-2003 02:30 PM
pthread signal problem xinwang00 Linux - Software 2 11-07-2003 12:05 PM


All times are GMT -5. The time now is 10:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration