Like Tree1Likes
  • 1 Post By anduril462

Joining thread before creating it

This is a discussion on Joining thread before creating it within the C Programming forums, part of the General Programming Boards category; Hi! I am new to the forum, and kind of new to c programming. Came a cross a problem while ...

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24

    Joining thread before creating it

    Hi!

    I am new to the forum, and kind of new to c programming.

    Came a cross a problem while writing my first multithreaded program:

    Code:
    while( cyclic task )
    {
        some stuff...
    
        if( condition )
        {
            pthread_mutex_lock( g_thread_finished_mutex );
            if(  g_thread_finished )
            {
                pthread_join( thread_ID, NULL );
                pthread_create( &thread_ID, NULL, thread_func, NULL );
            }
            pthread_mutex_unlock( g_thread_finished_mutex );
        }
        
        some other stuff...
    }
    thread_func is slow... It takes about 1s to finish and i only want one running at a time...

    g_thread_finished is an int that is initiated to 1. Then set to 0 in the beginning of thread_func, and back to 1 at the end.

    This throws a segmentation fault. But if i move the join call below the create call, everything works fine (Except that it looses all purpose).

    What is the problem? Shouldn't the pthread_join function simply return a NULL value the first time?

    Any ideas on how to solve this?

    Also. Do I need to lock the thread_finished mutex when reading the variable, or is it only necessary when writing?

    Any help is appreciated.

    //John
    Last edited by John Erlandsson; 05-16-2011 at 05:12 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    Without the rest of your code, I can't tell, but I would guess that, the first time through, thread_ID is uninitialized, and thus you are trying to join a non-existent thread. Something akin to closing an unopened, uninitialized file pointer. But with just what you posted, I can't say for sure. You have to create a thread before you can join it.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Could you join Facebook before they created it?
    Think about it....

    There ya go.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    Thanks for replying.

    I do realize the problem in joining something that doesn't exist.
    But I cant find an alternative.

    Code:
    void *ethercat_cyclic_function()
    {
        printf( "Starting cyclic thread...\n" );
        IO_digIn_t inputs;
        IO_digOut_t outputs;
    
        pthread_t vision_thread_ID;
    
        while( 1 )
        {
            time_t now = clock();
            time_t limit = now + CYCLE_TIME * CLOCKS_PER_SEC;
    
            /********************************
             * Update digital inputs     *
             ********************************/
            ecrt_master_receive( g_master );
            ecrt_domain_process( g_domain );
    
            unsigned char pdo_digIn1 = (unsigned char)EC_READ_U8( g_process_data + g_offInpMod1 );
            inputs.pushbutton = pdo_digIn1 >> 1;
    
            /********************************
             * Update digital outputs    *
             ********************************/
            unsigned char pdo_digOut1 = 0;
            pdo_digOut1 |= outputs.motor << 0 |
                           outputs.cylA  << 1 |
                           outputs.cylB  << 2 |
                           outputs.cylC  << 3;
            EC_WRITE_U8( g_process_data + g_offOutpMod1, pdo_digOut1 );
    
            ecrt_domain_queue( g_domain );
            ecrt_master_send( g_master );
    
            /********************************
             * Machine cycle               *
             ********************************/
            if( inputs.pushbutton )
            {
                pthread_mutex_lock( &g_vision_finished_LOCK );
                if( g_vision_finished )
                {
                    pthread_join( vision_thread_ID, NULL );
                    pthread_create( &vision_thread_ID, NULL, vision_thread, NULL );
                }
                pthread_mutex_unlock( &g_vision_finished_LOCK );
            }
    
            /********************************
             * Whait...                                       *
             ********************************/
            while( now < limit )
                now = clock();
        }
    }
    Code:
    void *vision_thread (void *arg)
    { 
        pthread_mutex_lock(&g_vision_finished_LOCK);
        g_vision_finished = 0;
        pthread_mutex_unlock(&g_vision_finished_LOCK);
        
        printf("Inside thread\n");
        sleep(10); //Debugging dummy
        
        pthread_mutex_lock(&g_vision_finished_LOCK);
        g_vision_finished = 1;
        pthread_mutex_unlock(&g_vision_finished_LOCK);
    
        //return (void *)result_pipe;
        return NULL;
    }
    The reason why i use a thread is to prevent the vision function from blocking the cyclic task.
    Should I simply skip the pthread_join and rely on my g_vision_finished variable?
    I am under the impression that I need to join the thread for it to finish completely.

    Thanks...

    //John
    Last edited by John Erlandsson; 05-16-2011 at 05:53 PM. Reason: Forgot to translate some comments...

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    Check out the tutorial here: https://computing.llnl.gov/tutorials/pthreads/.

    You don't really need to join:
    Quote Originally Posted by man pthread_join
    The pthread_join() function waits for the thread specified by thread to terminate. If that thread has already terminated, then pthread_join() returns immediately. The
    thread specified by thread must be joinable.
    Basically, join is like calling wait() on a forked child process. It causes the current thread to do nothing until the vision thread is done. As you said, you don't want to do this, you want your cyclic task to keep running. Your if (g_vision_finished) flag will be sufficient for not creating multiple vision threads, so just ditch the pthread_join call all together, you should be fine.

    EDIT: You may want a pthread_join after the cyclic work is all done, to let any last vision_thread work to be finished before your program exits.
    Last edited by anduril462; 05-16-2011 at 06:30 PM.
    Salem likes this.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Sweden
    Posts
    24
    Thank you!

    I will ditch the join function... And i will try a semaphore instead of the vision finished variable...

    //John

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with creating a thread
    By neeharikach in forum C Programming
    Replies: 1
    Last Post: 11-28-2010, 02:46 AM
  2. creating thread/sleep
    By it99 in forum C Programming
    Replies: 3
    Last Post: 06-17-2010, 11:04 PM
  3. Creating a thread in a class - C++
    By rag84dec in forum Windows Programming
    Replies: 1
    Last Post: 08-07-2009, 03:52 AM
  4. Creating A Thread W/ PThreads within an Object
    By HalNineThousand in forum Linux Programming
    Replies: 12
    Last Post: 03-28-2008, 02:57 PM
  5. Creating a thread -- Passing it a function
    By cjschw in forum C++ Programming
    Replies: 0
    Last Post: 08-05-2003, 02:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21