Solved

C++ multithreading code does not compile

Posted on 2013-01-16
4
406 Views
Last Modified: 2013-01-16
Hello experts!

I am using ubuntu and geanie.
I try to compile following code:


/******************************************************************************
* FILE: hello.c
* DESCRIPTION:
*   A "hello world" Pthreads program.  Demonstrates thread creation and
*   termination.
* AUTHOR: Blaise Barney
* LAST REVISED: 08/09/11
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS      5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0;t<NUM_THREADS;t++){
     printf("In main: creating thread %ld\n", t);
     rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
     if (rc){
       printf("ERROR; return code from pthread_create() is %d\n", rc);
       exit(-1);
       }
     }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}


I get error messages:


g++ -Wall -o "main" "main.cxx" (in directory: /home/jacek/Documents/cpp/wlasne/MultiThreading/4)
/tmp/ccvE3G41.o: In function `main':
main.cxx:(.text+0x85): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
Compilation failed.

What do I do wrong?

Thank you

panJames
0
Comment
Question by:panJames
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
Comment Utility
Hi panJames,

I guess you need to link 'pthread' library into your program. Try adding -lpthread to the command line ...

Hope that helps,

ZOPPO
0
 

Author Comment

by:panJames
Comment Utility
g++ -Wall -o -lpthread "main" "main.cxx" (in directory: /home/jacek/Documents/cpp/wlasne/MultiThreading/4)
g++: error: main: No such file or directory
Compilation failed.
0
 

Author Comment

by:panJames
Comment Utility
g++ -Wall -o -lpthread "%e" "%f"
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
Hi,

you should not add this directly after -o. The argument after -o is the name of the resulting executable.

ZOPPO
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now