gpt4 book ai didi

linux - pthread_create 将终止具有相同 tid 的前一个线程?

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:26 27 4
gpt4 key购买 nike

我发现在下面的代码中,每次接受新客户端时都会创建一个线程。在函数 pthread_client() 中没有退出日志。但是似乎没有通过命令 ps aux

创建多线程

我的理解是,每次创建一个具有相同tid的新线程时,具有相同tid的旧线程将被自动杀死,对吗?谢谢!

while(1){
fd = accept(...);
pthread_create(&tid1, NULL, (void *)pthread_client, (void *)arg);
pthread_detach(tid1);
}

最佳答案

My understanding is that each time a new thread is created with same tid, the old thread which has the same tid will be killed automatically, is it right? Thanks!

不,你错了!

来自手册:

 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread;

这意味着:在您的变量 tid 中,每次调用都会存储一个 值。旧值将被覆盖,只是代码中不再使用它。所以你总是得到一个新线程,但你的代码以后无法访问“旧”线程。但如您所见,您的代码只是从当前新创建的线程中分离出来,因此以后无需处理任何内容。

先前创建的线程只是继续。您不能通过调用 pthread_create 来终止正在运行的线程。

关于linux - pthread_create 将终止具有相同 tid 的前一个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55136298/

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