MarkWarriors
MarkWarriors

Reputation: 554

Kill a thread from another thread

I have this question:

Here is the sample code. I need that when I press 1, the other thread gets killed. (I must force the kill from one thread without putting a pthread_exit() in the other.)

pthread_t THR_INPUT, THR_QUEUE;

void *thr_in(void *null) {
    int opt;
    while(1){
        printf("1. Kill the other thread\n");
        scanf("%d", &opt);

        switch (opt)
        {
            case 1: 
                pthread_cancel(THR_QUEUE);
                pthread_exit(NULL);
            default:
                printf("ATTENZIONE: Scelta %i non corretta. Riprovare.\n",opt);

        }
    }
}

void *thr_qu(int reparto) {
    while(1){
    sleep(2);
    printf("I'm alive!");    
    }
}

int main(int argc, const char * argv[]){
    void *result;   

    printf("-------start-------\nMenu:\n");
    pthread_mutex_init(&mutex, NULL);
    pthread_create(&THR_INPUT, NULL, thr_in, NULL);
    pthread_create(&THR_QUEUE, NULL, thr_qu(reparto), NULL);
    pthread_join(THR_INPUT, &result);
    pthread_join(THR_QUEUE, &result);
    printf("---end---\n");
    exit(EXIT_SUCCESS);
}

I think about a solution but I don't know how clean it is; just do this:

int main(int argc, const char * argv[]){
    void *result;
    sem = semget(SEM_KEY, 0, 0); 
    pthread_mutex_init(&mutex, NULL);
    int pid=getpid();
    pid=fork();
    if(!pid){
    printf("-------START-------\nMenu:\n");
    pthread_create(&THR_INPUT, NULL, thr_in, NULL);
    pthread_create(&THR_QUEUE, NULL, thr_qu(reparto), NULL);
    pthread_join(THR_INPUT, &result);
    pthread_join(THR_QUEUE, &result);
    }
    else{
        wait(sem,0);
        pthread_cancel(THR_QUEUE);
        printf("---END---\n");
    }
    exit(EXIT_SUCCESS);
}

and putting a signal in the first thread that, when it is asked to exit, signal to the semaphore in main thread to do the pthread_cancel(). But it still not working, and i dont know why

Upvotes: 0

Views: 7391

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215547

You can use pthread_cancel, but it's only a request to the target thread, not a forcible kill. What distinguishes cancellation from other forms of requests, such as a flag communicated via a condition variable, is that:

  1. Cancellation is automatically acted upon at certain standard library calls, called cancellation points, unless the target thread has specifically suspended cancellation.

  2. Cancellation can work even when the target thread is blocked waiting for IO or certain other events that might never complete (in which case, it's the only way to stop them).

To use cancellation correctly, you must either make heavy use of pthread_cleanup_push at each level of resource allocation or major state change to ensure that all partial changes are backed out when a thread is cancelled, or you must block cancellation with pthread_setcancelstate except at certain points where it would be safe to act on cancellation with little or no cleanup effort.

Upvotes: 3

Related Questions