Find the answer to your Linux question:
Results 1 to 5 of 5
What all is in 'pthread_t thread' after pthread_create( pthread_t *restrict thread, ... ); ?? Should 'thread' stay in scope after a thread is created? That is, if a thread was ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Location
    The Sillicon Valley, California
    Posts
    28

    pthread_create question

    What all is in 'pthread_t thread' after pthread_create( pthread_t *restrict thread, ... ); ??

    Should 'thread' stay in scope after a thread is created?

    That is, if a thread was created, 'thread' must stay in existence until the thread ends, right?

    If 'thread' was defined in the function that called pthread_create,
    but then the function returned while the thread was still running,
    the data in 'thread' whould be on the stack, and vulnerable.

  2. #2
    Just Joined!
    Join Date
    Jun 2006
    Posts
    47
    You could receive all the information about pthread_create by inspecting the man pages ...
    Anyhow, pay attention to 2 last arguments of pthread_create: function and argument. A new thread will be created, and this thread will execute the following code:

    function( argument );
    exit( 0 );

    You see, when the thread function exits, the thread will exit - no magic. This has nothing to do with the future of a function that called pthread_create on the first place.
    2 more things:
    - the newly created thread and the creator thread will have common data section, i.e. common global variables area.
    - when the main() function exits, all the threads that were created on its behalf, here or there, will exit.

    NOTE:
    All this (default) behavior may be re-configured on demand.

  3. #3
    Just Joined!
    Join Date
    Jan 2010
    Location
    The Sillicon Valley, California
    Posts
    28
    Sorry, white_hound, but your reply had nothing to do with the original question.

    I shall succinctly rephrase:
    Does the data structure ‘thread’ which is the 1st argument of pthread_create(), need to remain in existence during the life of the thread, or is it just used by pthread_create() and not the thread.

    To err on the side of caution, I assume the former and not the later.

    I asked this because there is NOTHING about ‘thread’ in the man page of pthread_create().

  4. #4
    Just Joined!
    Join Date
    Jun 2006
    Posts
    47
    You are right, I didn't understand the pun: thread and 'thread' ...
    No, there is no need to keep the thread structure.
    All the essential structures are kept in the operating system kernel, and are not accessible to the user.

  5. #5
    Just Joined!
    Join Date
    Jan 2010
    Location
    The Sillicon Valley, California
    Posts
    28
    Oh, okay. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...