简体   繁体   English

C Pthread:通过一个线程优雅地杀死其他线程

[英]C Pthread: Kill other threads gracefully through one thread

I am working on a pthread problem in C. 我正在研究C中的pthread问题。

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. 问题的背景:有5个线程使用相同的功能,并且当共享内存中的核心数据达到上限时,所有这五个线程都应终止。 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. 该信号将在while循环运行时发出,我尝试在以下位置接受该信号,然后终止所有线程。

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. 老实说,我需要做的是优雅地结束所有线程,并且我需要它们返回主线程以获取thread_join()和空闲内存,而不是简单地退出程序。 So that's why I did not use exit() here. 这就是为什么我在这里不使用exit()的原因。

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. 您可以从要终止所有其他线程的线程中使用pthead_kill。 Man page is at http://linux.die.net/man/3/pthread_kill . 手册页位于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. http://linux.die.net/man/7/signal有更多详细信息。

The easiest solution is probably to have a single global boolean variable, initially initialized to "false". 最简单的解决方案可能是使用单个全局布尔变量,该变量最初初始化为“ false”。 All the threads check if this variable is "false" or "true", and if it's "true" they terminate. 所有线程都检查此变量是“ false”还是“ true”,如果是“ true”,则它们终止。

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. 当一个线程注意到所有线程都应终止时,它只需将此标志设置为“ true”,其他线程迟早会注意到这一点。

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). 您可以在线程函数中的多个位置检查此退出条件,尤其是当某个线程正在等待当前活动线程的锁(设置退出条件的线程)时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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