Thread: Pthreads

  1. #1
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    Unhappy Pthreads

    Hi, what is wrong with this code?
    Code:
    #include <pthread.h>
    #include <stdio.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);
          }
       }
       pthread_exit(NULL);
    }
    Its output should look like this:
    In main: creating thread 0
    In main: creating thread 1
    Hello World! It's me, thread #0!
    In main: creating thread 2
    Hello World! It's me, thread #1!
    Hello World! It's me, thread #2!
    In main: creating thread 3
    In main: creating thread 4
    Hello World! It's me, thread #3!
    Hello World! It's me, thread #4!

    I copied this from the tutorial on POSIX threads.
    Arduino rocks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is it you think it's supposed to do, that's different from what actually happens?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Quote Originally Posted by quzah View Post
    What is it you think it's supposed to do, that's different from what actually happens?


    Quzah.
    There is an error, when i compile it like this:
    gcc d.c -pthread d

    d.c: In function ‘main’:
    d.c:23: warning: incompatible implicit declaration of built-in function ‘exit’

    And like this:
    gcc d.c -o d

    d.c: In function ‘main’:
    d.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
    /tmp/ccCnNvZ9.o: In function `main':
    d.c.text+0x82): undefined reference to `pthread_create'
    collect2: ld returned 1 exit status
    Arduino rocks!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    exit is in <stdilb.h>, so you need to include that. And I would have thought that it was -lpthread, not -pthread?

  5. #5
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    ok

    Quote Originally Posted by tabstop View Post
    exit is in <stdilb.h>, so you need to include that. And I would have thought that it was -lpthread, not -pthread?
    But it is still saying there is no such file or directory...
    Arduino rocks!

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    <stdlib.h>, not <stdilb.h>
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Quote Originally Posted by Dino View Post
    <stdlib.h>, not <stdilb.h>
    yes...i know -.-
    Arduino rocks!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dino View Post
    <stdlib.h>, not <stdilb.h>
    Oops, sorry. There's probably a reason I should put code things in Courier.

  9. #9
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Quote Originally Posted by tabstop View Post
    Oops, sorry. There's probably a reason I should put code things in Courier.
    what do you mean?
    Arduino rocks!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by yann View Post
    what do you mean?
    Probably because even after Dino pointed it out it took a long time to tell the difference between <stdlib.h> and <stdilb.h>.

  11. #11
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    now, we were talking about pthread error...
    Arduino rocks!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Repost your code with the correct changes, and any errors or warnings you are getting, and try again. The thread is way too off topic / confused to know what it is you really want from us. Also, you typically want to include standard header files before non-standard ones.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using pthreads with an API that uses pthreads
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 02:47 PM
  2. Pthreads performance
    By C_ntua in forum C Programming
    Replies: 42
    Last Post: 06-17-2008, 11:29 AM
  3. Replies: 3
    Last Post: 04-16-2007, 12:02 PM
  4. Difference between win32 and linux pthreads
    By philipsan in forum C Programming
    Replies: 1
    Last Post: 02-07-2006, 04:57 PM
  5. pthreads and resources
    By ubermensch in forum C Programming
    Replies: 2
    Last Post: 02-07-2006, 02:27 AM