Thread: Question about pthread_create and pthread_join function arguments

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    5

    Question about pthread_create and pthread_join function arguments

    In the documentation that I found on the internet for pthread_join

    the function declaration is the following:

    int pthread_join(pthread_t thread, void **retval);

    where pthread_create is:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    void *(*start_routine) (void *), void *arg);

    Now my question is why does the create function require a pointer argument for the thread(first argument) and for the join function it does not. My guess it has something to do with the start routine used in pthread_create but I'm not quite sure.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because pthread_create needs to return two values.
    - the ID of the created thread, which will be stored in the first parameter
    - the success / failure status of the create.

    This information is available in any of the online manuals.
    DESCRIPTION
    ...
    Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent calls to other
    pthreads functions.
    ...
    RETURN VALUE
    On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Think about pthread_create() the same way as fork() for threads, instead of processes. By default, pthread_create() creates a new thread tied to the thread which created it... You can create "detached" threads using the attribute argument:

    Code:
    extern void *thread_routine( void * );
    pthread_attr_t attr;
    pthread_t tid;  // thread id;
    
    pthread_attr_init( &attr );
    pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
    pthread_create( &tid, &attr, thread_routine, NULL );
    // now the new thread is running "detached".
    pthread_attr_destroy( &attr ); // we don't need this anymore...
    ...
    Another way to do it is calling pthread_detach(), inside the thread routine.

    This way this new thread cannot be "joined" to the primary thread which created it. Why cannot be "joined"? Because "joinable" threads don't have their resources freed, automatically, if they aren't "joined"... Detached threads free their resources in thread termination.

    pthread_join() is used a a barrier, where the current thread waits for the termination of the other running "joinable" threads.

    Notice pthread_create() takes a pointer to a thread "descriptor" (where an ID for the new thread will be writen, like PID, for threads), a pointer to thread attributes (which you can tune using pthread_attr_* functions, or use the default attributes, passing NULL), a pointer to the routine that will run in this new thread and a pointer to the argument pthread_create will pass to this function.

    pthread_join(), as I said before, will simply wait for thread termination and optionally, returns the pointer to the thread function return value, freeing thread resources in the process. So it needs only the thread "ID" and the pointer which will receive the result (or NULL if you don't care for results).

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    5
    Quote Originally Posted by flp1969 View Post
    Think about pthread_create() the same way as fork() for threads, instead of processes. By default, pthread_create() creates a new thread tied to the thread which created it... You can create "detached" threads using the attribute argument:
    OK, that helps a lot with understanding threads. So, when we call the pthread_create() function are we creating a child of the main thread? just as a process would? so, it returns 0 when child returns from the start_routine, unless there is pthread_join() function which makes the new thread to wait for the others threads to finish?

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by hamsasimon View Post
    OK, that helps a lot with understanding threads. So, when we call the pthread_create() function are we creating a child of the main thread? Just as a process would?
    Yes!

    Quote Originally Posted by hamsasimon View Post
    so, it returns 0 when child returns from the start_routine, unless there is pthread_join() function which makes the new thread to wait for the others threads to finish?
    No! pthread_create() will return 0 if it was able to create the new thread. The new thread runs in a context independent from the main thread... IF this new thread is "joinable", calling ptherad_join() will wait for the other thread termination and set the second argument with the return value from this terminating thread... Notice your thread function has the prototype:
    Code:
    void *thread_func( void *arg );
    And pthread_join has:
    Code:
    int pthread_join( pthread_t tid, void **ret );
    The second argument is a pointer to a pointer which will receive the resulting pointer from terminating thread.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    A good reference: pthread tutorial

  7. #7
    Registered User
    Join Date
    May 2019
    Posts
    5
    Quote Originally Posted by Salem View Post
    Because pthread_create needs to return two values.<br>
    - the ID of the created thread, which will be stored in the first parameter<br>
    - the success / failure status of the create.<br>
    <br>
    This information is available in any of the online manuals.
    Thank you, that makes sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread_create arguments
    By JOCAAN in forum C Programming
    Replies: 11
    Last Post: 03-24-2011, 06:18 PM
  2. Arg struct in pThread function [pthread_create()]
    By grytskiv in forum C Programming
    Replies: 1
    Last Post: 03-11-2011, 12:39 PM
  3. Yet another function arguments question
    By audinue in forum C Programming
    Replies: 3
    Last Post: 10-20-2008, 05:09 AM
  4. Trouble with pthread_create and pthread_join
    By Yasir_Malik in forum Linux Programming
    Replies: 2
    Last Post: 10-02-2003, 01:48 PM
  5. pthread_create Function
    By Divakarp in forum C Programming
    Replies: 0
    Last Post: 02-24-2003, 09:30 AM

Tags for this Thread