gpt4 book ai didi

c - 尽管运行 pthread_exit,分离的线程不会退出?

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:25 25 4
gpt4 key购买 nike

几天来我一直在处理线程池中的问题。我尝试了各种不同的方法,但似乎无法解决问题。我制作了一个重现该问题的简单版本。

代码:

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>


struct bsem_t bsem;
pthread_t threads[2];


/* Binary semaphore */
typedef struct bsem_t {
pthread_mutex_t mutex;
pthread_cond_t cond;
int v;
} bsem_t;

void bsem_post(bsem_t *bsem) {
pthread_mutex_lock(&bsem->mutex);
bsem->v = 1;
pthread_cond_broadcast(&bsem->cond);
pthread_mutex_unlock(&bsem->mutex);
}

void bsem_wait(bsem_t *bsem) {
pthread_mutex_lock(&bsem->mutex);
while (bsem->v != 1) {
pthread_cond_wait(&bsem->cond, &bsem->mutex);
}
bsem->v = 0;
pthread_mutex_unlock(&bsem->mutex);
}


/* Being called by each thread on SIGUSR1 */
void thread_exit(){
printf("%u: pthread_exit()\n", (int)pthread_self());
pthread_exit(NULL);
}


/* Startpoint for each thread */
void thread_do(){

struct sigaction act;
act.sa_handler = thread_exit;
sigaction(SIGUSR1, &act, NULL);

while(1){
bsem_wait(&bsem); // Each thread is blocked here
puts("Passed semaphore");
}

}


/* Main */
int main(){

bsem.v = 0;

pthread_create(&threads[0], NULL, (void *)thread_do, NULL);
pthread_create(&threads[1], NULL, (void *)thread_do, NULL);
pthread_detach(threads[0]);
pthread_detach(threads[1]);
puts("Created threads");

sleep(2);

pthread_kill(threads[0], SIGUSR1);
pthread_kill(threads[1], SIGUSR1);

puts("Killed threads");

sleep(10);

return 0;
}

代码的作用是创建两个线程。两个线程都等待二进制信号量 (bsem_wait)。然后,当他们等待时,我向两者发送一个 SIGUSR1 信号,导致 pthread_exit() 在每个线程上执行。在我的终端上它显示一切都按计划进行..

输出:

Created threads
Killed threads
2695145216: pthread_exit()
2686752512: pthread_exit()

问题

虽然输出看起来是正确的,但使用 pstree 显示两个线程中只有一个死了。另一个线程保持事件状态,直到整个程序退出。这是为什么?


更新

用普通信号量替换我的自定义二进制信号量似乎无缘无故地解决了这个问题..

#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <semaphore.h>

sem_t sem;
pthread_t threads[2];


/* Caller thread will exit */
void thread_exit(){
printf("%u: pthread_exit()\n", (int)pthread_self());
pthread_exit(NULL);
}


/* Startpoint for each thread */
void* thread_do(){

struct sigaction act;
act.sa_handler = thread_exit;
sigaction(SIGUSR1, &act, NULL);

while(1){
sem_wait(&sem); // Each thread is blocked here
puts("Passed semaphore");
}

}


/* Main */
int main(){

sem_init(&sem, 0, 0); // Normal semaphore

pthread_create(&threads[0], NULL, thread_do, NULL);
pthread_create(&threads[1], NULL, thread_do, NULL);
pthread_detach(threads[0]);
pthread_detach(threads[1]);
puts("Created threads in pool");

sleep(2);

//PROBLEM
pthread_kill(threads[0], SIGUSR1);
pthread_kill(threads[1], SIGUSR1);

puts("Destroyed pool");

sleep(10);

return 0;
}

最佳答案

你不能从这里到达那里

pthread_exit() 未在 signal(7) 手册页的“信号安全功能”中列出。重写您的代码以在信号处理程序之外调用 pthread_exit。

关于c - 尽管运行 pthread_exit,分离的线程不会退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27733085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com