c++ - sigqueue 可以与 pthreads 一起使用吗?

标签 c++ c linux pthreads posix

是否可以使用 sigqueue 代替 pthread_kill()在多线程中,因为我想在 sigqueue 中使用 (const union sigval value)。我没有 pthread_sigqueue()在我的 Linux 中。

最佳答案

在 Linux 下,可以使用 gettid() 从内核中提取线程特定的 pid_t系统调用,然后可以与 sigqueue() 一起使用.然而,这是 Linux 特有的并且非常不可移植。

由于 gettid() 很可能无法通过 Linux 的 libc 使用,您可能希望使用以下代码来实现它:

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>

pid_t gettid(void) 
{
  return syscall(SYS_gettid);
}

关于c++ - sigqueue 可以与 pthreads 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21651333/

相关文章:

C++:具有 STL 类型和自定义类型的优雅 "polymorphism"?

c - 如何在 MacOS 11.1 上使用 Homebrew 安装 libuuid?

linux - if 条件出错

linux - 将文件分类到同名文件夹中的 Bash 脚本

python - 如何将 Python 代码转换为 C++

c++ - 在 SFML 的 TGUI 库中验证输入

c++ - 将 char 转换为 unsigned char

c - 时间 = C 中的距离与速度

c - 如何创建多个进程

linux - Jenkins 容器 - 无效的引用格式