我想确定特定线程是否“存在”。
pthread_kill()似乎适合这项任务,至少根据其手册页来看是这样。
如果sig为0,则不发送信号,但仍执行错误检查。
或者,正如我的系统手册页所述:
如果sig为0,则不发送信号,但仍进行错误检查;这可用于检查线程 ID 是否存在。
但是,当我尝试传入未初始化的 时pthread_t,应用程序总是出现 SEGFAULT。
深入研究这一点,pthread_kill.c(来自我的工具链)的以下代码片段似乎没有进行错误检查,只是尝试取消引用threadid(取消引用位于pd->tid)。
int
__pthread_kill (threadid, signo)
pthread_t threadid;
int signo;
{
struct pthread *pd = (struct pthread *) threadid;
/* Make sure the descriptor is valid. */
if (DEBUGGING_P && INVALID_TD_P (pd))
/* Not a valid thread handle. */
return ESRCH;
/* Force load of pd->tid into local variable or register. Otherwise
if a thread exits between ESRCH test and tgkill, we might return
EINVAL, because pd->tid would be cleared by the kernel. */
pid_t tid = atomic_forced_read (pd->tid);
if (__builtin_expect (tid <= 0, 0))
/* Not a valid thread handle. */
return ESRCH;
Run Code Online (Sandbox Code Playgroud)
我们甚至不能依赖零作为一个好的初始化器,因为以下原因:
int
__pthread_kill (threadid, signo)
pthread_t threadid;
int signo;
{
struct pthread *pd = (struct pthread *) threadid;
/* Make sure the descriptor is valid. */
if (DEBUGGING_P && INVALID_TD_P (pd))
/* Not a valid thread handle. */
return ESRCH;
/* Force load of pd->tid into local variable or register. Otherwise
if a thread exits between ESRCH test and tgkill, we might return
EINVAL, because pd->tid would be cleared by the kernel. */
pid_t tid = atomic_forced_read (pd->tid);
if (__builtin_expect (tid <= 0, 0))
/* Not a valid thread handle. */
return ESRCH;
Run Code Online (Sandbox Code Playgroud)
此外,我在链接的手册页中注意到以下内容(但不在我的系统上):
POSIX.1-2008 建议,如果实现在其生命周期结束后检测到线程 ID 的使用,则 pthread_kill() 应返回错误 ESRCH。在检测到无效线程 ID 的情况下,glibc 实现会返回此错误。 但还要注意,POSIX 表示尝试使用生命周期已结束的线程 ID 会产生未定义的行为,并且尝试在调用 pthread_kill() 时使用无效的线程 ID 可能会导致分段错误。
正如R.. 此处所述,我要求的是可怕的未定义行为。
看来该手册确实具有误导性——尤其是在我的系统上。
pthread_kill())pthread_t类型变量,即使我们必须自己捕获它们?我怀疑答案是雇用pthread_cleanup_push()并保留is_running自己的旗帜,但想听听其他人的想法。
我想我在开车回家时意识到了这一点,我怀疑其他许多人也可能会发现这很有用......
看起来我一直将工作人员(线程)和任务(线程正在做什么)视为同一事物,但事实上,它们并非如此。
正如我已经从问题中的代码片段中确定的那样,询问“该线程是否存在”是不合理的,因为它pthread_t可能只是一个指针(它肯定在我的目标上)。几乎可以肯定这是一个错误的问题。
进程 ID、文件句柄、内存等也是如此malloc()……它们不使用唯一且从不重复的标识符,因此不是可以测试其存在的唯一“实体”。
我在问题中提出的怀疑可能是正确的 - 我将不得不使用诸如任务is_running标志之类的东西(而不是线程)。
我考虑过的一种方法是使用初始化为 1、 和 的信号量sem_trywait(),sem_post()如下pthread_cleanup_push()例所示(为简洁起见,缺少清理)。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h>
struct my_task {
sem_t can_start;
pthread_t tid;
/* task-related stuff */
};
void *my_task_worker(void *arg) {
struct my_task *task = arg;
pthread_cleanup_push(sem_post, &(task->can_start));
fprintf(stderr, "--- task starting!\n");
usleep(2500000);
fprintf(stderr, "--- task ending!\n");
pthread_cleanup_pop(1);
return NULL;
}
void my_task_start(struct my_task *task) {
int ret;
ret = sem_trywait(&(task->can_start));
if (ret != 0) {
if (errno != EAGAIN) {
perror("sem_trywait()");
exit(1);
}
fprintf(stderr, ">>> task already running...\n");
return;
}
ret = pthread_create(&(task->tid), NULL, my_task_worker, task);
if (ret != 0) {
perror("pthread_create()");
exit(1);
}
fprintf(stderr, ">>> started task!\n");
return;
}
int main(int argc, char *argv[]) {
int ret;
struct my_task task;
int i;
memset(&task, 0, sizeof(0));
ret = sem_init(&(task.can_start), 0, 1);
if (ret != 0)
{
perror("sem_init()");
return 1;
}
for (i = 0; i < 10; i++) {
my_task_start(&task);
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
>>> started task!
--- task starting!
>>> task already running...
>>> task already running...
--- task ending!
>>> started task!
--- task starting!
>>> task already running...
>>> task already running...
--- task ending!
>>> started task!
--- task starting!
>>> task already running...
>>> task already running...
--- task ending!
>>> started task!
--- task starting!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1896 次 |
| 最近记录: |