6

How do you close a thread, when you done? like making sure nothing is open anymore or runing?

so far i know how to open it, but .. not how to close it right

int  iret1; 
pthread_t thread1;
char *message1;

void *multithreading1( void *ptr ) {
    while (1) {
        // Our function here
    }   
}

int main (int argc, char * const argv[]) {
    if( (iret1=pthread_create( &thread1, NULL, multithreading1, (void*) message1)) )
    {
        printf("Thread creation failed: %d\n", iret1);
    }
    return 0;
}
Danubian Sailor
  • 1
  • 38
  • 145
  • 223
user1417815
  • 445
  • 3
  • 7
  • 16

2 Answers2

7

To do this, you either return from the thread function (multithreading1) or call pthread_exit().

For more information, see POSIX Threads Programming.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
7

"How do you close a thread, when you done?"
Either by just simple returning from that function or calling pthread_exit function.

Note that calling return also causes the stack to be unwound and variables declared within start routine to be destroyed, thus it's more preferable than pthread_exit function:

An implicit call to pthread_exit() is made when a thread other than the thread in
which main() was first invoked returns from the start routine that was used to
create it. The function's return value shall serve as the thread's exit status.

For more information also have a look at: return() versus pthread_exit() in pthread start functions

"making sure nothing is open anymore or runing"
Instead of making sure whether your thread is still running, you should wait for its termination by using pthread_join function.

Here's an example:

void *routine(void *ptr) {
    int* arg = (int*) ptr; // in C, explicit type cast is redundant
    printf("changing %d to 7\n", *arg);
    *arg = 7;
    return ptr;
}

int main(int argc, char * const argv[]) {
    pthread_t thread1;
    int arg = 3;
    pthread_create(&thread1, NULL, routine, (void*) &arg);

    int* retval;
    pthread_join(thread1, (void**) &retval);
    printf("thread1 returned %d\n", *retval);
    return 0;
}

output

changing 3 to 7
thread1 returned 7
Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • Wow, it eat much, much less power and do just what i need! thanks LihO! :D – user1417815 May 30 '12 at 18:15
  • i'm getting " invalid conversion from 'void*' to 'int* " from: int* arg = ptr;, but ice explaining ! – user1417815 May 30 '12 at 18:49
  • 2
    @user1417815: Check my answer now :) You are getting an error, because in C++ type cast is required while in pure C it can be omitted. – LihO May 30 '12 at 18:51
  • So return is better then exit? well nice explain and coding :) ! thanks alot – user1417815 May 30 '12 at 19:04
  • @user1417815: Yes, it is better to return from start routine than terminate it by calling `pthread_exit()`. Note that there is implicit call to `pthread_exit()` when it returns. I've also updated my answer again. – LihO May 30 '12 at 19:26
  • I'm sure using it, you explained everything that i need to get started on something new, thanks alot LihO ;-) – user1417815 May 30 '12 at 20:12