Hello,

this is a simple model for a simple program that will spawn a thread to run a given option in a menu (only option 1 and 2 run threads). The loop will need to wait for a thread to complete, so it uses the thread_join function

pthread_t newthread;

while ( 1 ) {

 scanf ("%d", &option );

 switch ( option ) {

    case 1:
              pthread_create ( newthread, NULL, function (1), NULL );
              break;
    case 2:
              pthread_create ( newthread, NULL, function (2), NULL );
              break;
     case 3:
             printf("message");
             break;
     case 4:
            exit (0);
            break;


 }

  thread_join ( newthread, NULL );
}

Well the thing is, the code will get there from option 3 also, so I wanted to know if this can cause problems (since option 3 doesn't create the thread).

Thanks for your time and help.

If you want to be safe, 'option' is still in scope, and you could just put an

if(option<3)

check to keep the thread_join from being called.

That'd be one way to take care of it, but it doesn't answer your question quite as you wanted...

If you want to be safe, 'option' is still in scope, and you could just put an

if(option<3)

check to keep the thread_join from being called.

That'd be one way to take care of it, but it doesn't answer your question quite as you wanted...

Yeah, well, I think that putting pthread_join call after the pthread_Create call would work ok and would spare an if.

Just wanted to know if the other solution would clean everything.

Thanks for your comment , :).

This article has been dead for over six months. Start a new discussion instead.