Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close a thread when done with it

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;
}
like image 641
user1417815 Avatar asked May 30 '12 17:05

user1417815


People also ask

How do you close a thread in Python?

We can close a thread by returning from the run function at any time. This can be achieved by using the “return” statement in our target task function. If the threading. Thread class has been extended and the run() function overridden, then the “return” statement can be used in the run() function directly.

How do you end a thread when the main program ends?

Use the atexit module of Python's standard library to register "termination" functions that get called (on the main thread) on any reasonably "clean" termination of the main thread, including an uncaught exception such as KeyboardInterrupt . Such termination functions may (though inevitably in the main thread!)

Can you terminate a thread?

A thread automatically terminates when it returns from its entry-point routine. A thread can also explicitly terminate itself or terminate any other thread in the process, using a mechanism called cancelation.


1 Answers

"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
like image 196
LihO Avatar answered Sep 19 '22 23:09

LihO