简体   繁体   中英

C Pthread: Kill other threads gracefully through one thread

I am working on a pthread problem in C.

The background of the problem: There are 5 threads using the same function, and when the core data in the shared memory hit the upper bound, all these five threads should terminate. I use semaphore to make sure only one of them executes the core data, which means only one of the five will get the end signal and then it will tell the rest to terminate. The code I wrote to achieve is:

#define THREAD_NUM 5;
int thread[THREAD_NUM];
sem_t s;       /*semaphore to synchronize*/
sem_t empty;   /*keep check of the number of empty buffers*/
sem_t full;    /*keep check of the number of full buffers*/


void com_thread(*prt){ // I pass the id of the thread using prt
     while(1){
        sem_wait(&full)
        sem_wait(&s)
        ...do something
        sem_post(&s)
        sem_post(&empty)
     }   
}

The signal will come while the while loop is running, and I tried to accept the signal at following position and then terminate all the threads.

To be honest, what I need to do is end all of the threads gracefully, and I need them to return to the main thread for thread_join() and free memory rather than simply exiting the program. So that's why I did not use exit() here.

The main idea below is terminating the other 4 threads when one of them got the signal. After that it would terminate itself.

However, it does not work as I expected.

#define THREAD_NUM 5;
int thread[THREAD_NUM];
sem_t s;       /*semaphore to synchronize*/
sem_t empty;   /*keep check of the number of empty buffers*/
sem_t full;    /*keep check of the number of full buffers*/


void com_thread(*prt){ // I pass the id of the thread using prt
     while(1){
        sem_wait(&full)
        sem_wait(&s)

        if(signal){
            int i;
            int id = *((int*) prt);
            for (i=0;i<THREAD_NUM;i++){
                if(i != id)
                pthread_exit(&thread[i]);
            }
            pthread_exit(&thread[id]);
        }

        ...do something
        sem_post(&s)
        sem_post(&empty)
     }   
}

Can anyone help me with that? Or, if there is a better way to achieve this? Thanks in advance :)

You can use pthead_kill from the thread you want to terminate all other threads. Man page is at http://linux.die.net/man/3/pthread_kill . You should choose signal carefully if you want graceful termination. http://linux.die.net/man/7/signal has more details.

The easiest solution is probably to have a single global boolean variable, initially initialized to "false". All the threads check if this variable is "false" or "true", and if it's "true" they terminate.

When a single thread notices that all threads should be terminated it simply sets this flag to "true", and the other threads will notice that sooner or later.

You can check for this exit-condition in multiple places in the thread function, especially if some thread is waiting for a lock from the currently active thread (the one that sets the exit condition).

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