简体   繁体   中英

Can't terminate the thread using pthread_exit

I cant terminate the thread, it keeps sending things even after I close the terminal...

void *RTPfun(void * client_addr);

int main(int argc, char *argv[])
{
   pthread_t RTPthread;
   pthread_create(&RTPthread, NULL, &RTPfun, (void*)client_addr);
   ...
   ...   
   pthread_exit(&RTPfun);
   return 0;
 }

 void *RTPfun(void * client_addr)
 {
     ...
     ...
     return 0;
 }

Can someone tell me what am I doing wrong? Thanks!

pthread_exit kills your current thread.

Notice, that if you kill the main thread as you do, it does not terminate the process. Other threads keep running.

You probably want to use pthread_cancel .

More generally though, killing threads is a bad idea. Correct way is to ask your threads politely to terminate and wait till they do.

If you call exit() from main, it will terminate main thread with all other thread.

If you call the method pthread_exit() from your main it will terminate main thread and let other thread will run continuously.

In your case you are calling pthread_exit() from main so your main thread get terminated, and other thread running until thread gets finish the job.

To cancel thread Add Below in RTPfun and add pthread_cancel in main.

/* call this when you are not ready to cancel the thread */ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

/* call this when you are ready to cancel the thread */ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

Working sample code:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *RTPfun(void * client_addr);

int main(int argc, char *argv[])
{
   pthread_t RTPthread;
    int client_addr;
   pthread_create(&RTPthread, NULL, &RTPfun, (void*)client_addr);
   sleep(2);

   pthread_cancel(RTPthread);
   pthread_join(RTPthread, NULL);

   return 0;
 }

 void *RTPfun(void * client_addr)
 {
    int count = 0;
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    while(1) {
        if(count > 10) {
                printf("thread set for cancel\n");
                pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);     
        }
        sleep(1);
        printf("count:%d\n", count);
        count ++;
    }
    return 0;
 }

Used sleep in code for just understanding.

My understanding:

From "man pthread_exit" clearly talks about the description and rational behavior.

we will need to clean-up all respected resource that are been used in the thread created. " The pthread_exit() function shall terminate the calling thread and make the value value_ptr available to any successful join with the terminating thread."

we shall pass "value_ptr" to exit(value_ptr) --> this will help to analyse what was the results of exit process.

obviously exit() - call exiting from the process, will release resources that used for.

In other way, you can create pthread in detached state, this attribute will reclaim the resources implicitly when pthread_exit . Refer "man pthread_detach".

We don't need to use pthread_join .. either go for pthread_detach again its simple go ahead set attribute .

/* set the thread detach state */ ret = pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED);

Thanks. Sankar

Your pthread_exit() exits the current thread. The parameter is used to pass a return value to any thread that then wants to join with it - and not to specify which thread to exit, as your code is implying.

The nicest way to do what you want is to use pthread_cancel() from your main thread. It takes the thread to cancel as a parameter, and then sends a cancellation request to that thread. Notice though, that by default cancellation is deferred, so your thread will keep on running until it hits a function that is a cancellation point - if you don't use any of those functions, you can insert an explicit cancellation point with a call to pthread_testcancel() .

If you need to do some cleanup (for instance to free allocated memory, unlock mutexes, etc), you can insert a cleanup handler that is automatically called when canceling the thread. Have a look at pthread_cleanup_push() for this.

You can also set your thread to use asynchronous cancellation - your thread can then be canceled immediately, without hitting a cancellation point. However, asynchronous cancellation should only be used if you aren't using any system calls at all (that is, it's okay if you're purely doing calculations on already available data - but not if you're for instance using printf , file I/O, socket communication or similar), as otherwise you'll risk your system ending up in an inconsistent state.

After calling pthread_cancel() , your main thread should call pthread_join() on the canceled thread, to make sure all thread resources are cleaned up (unless you create the thread as detached).

You can of course also just have a shared doExit flag between the two threads, that the main thread can set, and which the other thread looks at from time to time. It's basically a manual way of using pthread_cancel() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM