Thread: question about the pthread.

  1. #1
    Registered User
    Join Date
    Mar 2008

    Post question about the pthread.

    There are codes about pthread in C
    1.#include <pthread.h>
    2.#include <stdlib.h>
    3.#include <unistd.h>
    4.void *thread_function(void *arg) {  
    5.    printf("Thread says hi!\n");    
    6.  return NULL;
    7.} main(void) {
    9.  pthread_t mythread;
    10.  if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
    11.   printf("error creating thread.");
    12.    abort();
    13.  }
    14.  if ( pthread_join ( mythread, NULL ) ) {
    15.   printf("error joining thread.");
    16.    abort();
    17.  }
    18.  exit(0);
    After compiling and executing i get what i expect
    red 327 % gcc -o thread.out thread.c -lpthread
    red 328 % thread.out
    Thread says hi!
    red 329 %
    However, if I delete the second statement at line 14 and after compiling and executing i get
    red 329 % gcc -o thread.out thread.c -lpthread
    red 330 % thread.out
    red 331 %
    My question is why the result is like this after deleting pthread_join(mythread,NULL) and what does pthread_join do.Thanks

  2. #2
    Registered User
    Join Date
    Sep 2007
    pthread_join() is similar in function to waitpid(). It (pthread_join) waits for the specified thread to terminate. If you don't call pthread_join(), but just exit(), then you have a race. When exit() is called, all threads are killed off (at least on modern Linuxes; if you're not running Linux, then apparently this holds for whatever you run as well). If exit() is called before the thread is able to print, you'll get no output.

    It's possible that you'll actually see it print out, depending on various factors. You can force it by sleeping for a while in the main thread before you exit. The join just forces the main thread to wait until the other thread has terminated, however long that takes.

    I'm not sure if POSIX specifies the exit() behavior that Linux shows, and I'm too lazy to look it up.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Did you first look up what the API does? I.e., pthread_join.

    The function is used to wait for the given thread to finish. So, in your original code, main creates a thread, waits for it to finish, then exits. This is why you see the thread print out the message.

    If you remove the call to this function, then main does not (have to) wait for the thread to finish. So, main creates a thread, then exists--it doesnt wait for the thread to finish executing. However, due to the non-deterministic way that CPU schedulers work, the message may or may not be printed. That is, depending on how the process/threads are scheduled, the new thread may get CPU time and be able to execute the printf statement. Other times, (more likely/often?) main will already be allotted CPU time and be able to exit right away.

    To see this, just make "main" take longer. For example, in place of the join function call, put a timer for a 1 second, or even an empty loop (ie int x = 100000; while ( x-- > 0); ), and this should give the other thread time to execute, before main exits.

    N.B: Ive never used pthreads, my only experience is process management in Unix, i.e. fork, etc. Ive just used intuition to imply what I said above. I imagine its quite accurate, though.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    It's an error not to call pthread_join or pthread_detach on an "attached"/joinable thread. To detach the thread, either create the thread detached (via attribute) or call pthread_detach.

    Returning from main() is the same as calling exit() - which kills all threads. If you don't want that behavior, then call pthread_exit a the end of main. pthread_exit never returns, thus main never returns. This will allow any remaining (detached) threads to continue to run. When all threads return the process will exit as if exit(0) was called.


  5. #5
    Registered User
    Join Date
    Mar 2008
    Thanks for your answers all.They are very useful to me know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick pthread question - starting paused/suspended
    By NightWolf8800 in forum C Programming
    Replies: 3
    Last Post: 10-24-2009, 08:15 AM
  2. Pthread question
    By gundamz2001 in forum C Programming
    Replies: 3
    Last Post: 09-16-2009, 04:04 AM
  3. The Pthread Hell
    By matott in forum Linux Programming
    Replies: 1
    Last Post: 04-10-2005, 05:59 AM
  4. pthread question
    By rotis23 in forum Linux Programming
    Replies: 10
    Last Post: 04-07-2004, 02:57 AM
  5. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM