C++ 调用timer_create()时的新线程
我正在研究调用函数timer_create()时如何获取线程ID。 我观察到,每次调用timer_create()时,都会创建一个新线程(主进程的子线程)。我用ps-eL | grep验证了这一点 我需要得到相同的TID(子线程ID)显示的ps-eL在我的程序内使用定时器创建。 下面的代码:如何在程序中获得TID 18018 我研究了所有帖子,每个帖子都提到在调用通知函数而不是调用timer_create()时创建了一个新线程 我非常感谢你的帮助 代码:C++ 调用timer_create()时的新线程,c++,c,linux,multithreading,C++,C,Linux,Multithreading,我正在研究调用函数timer_create()时如何获取线程ID。 我观察到,每次调用timer_create()时,都会创建一个新线程(主进程的子线程)。我用ps-eL | grep验证了这一点 我需要得到相同的TID(子线程ID)显示的ps-eL在我的程序内使用定时器创建。 下面的代码:如何在程序中获得TID 18018 我研究了所有帖子,每个帖子都提到在调用通知函数而不是调用timer_create()时创建了一个新线程 我非常感谢你的帮助 代码: 未指定在调用超时处理程序之前是否存在线
未指定在调用超时处理程序之前是否存在线程。当然,一致性实现可以推迟线程的创建,直到计时器过期。看起来像PID的数字线程ID的存在也是一个实现细节,您不应该使用它
你能解释一下你想完成什么吗?当然有更好的方法…经过多次测试,我发现在调用timer\u create()时创建了一个子线程。我正在尝试在程序中获取该线程ID。我将使用该线程ID为该线程设置调度选项。是的,在调用timer\u create()之后和调用timeoutHandler()之前存在一个线程。@LinuxNewbie如果您需要,您可以指定一个pthread\u attr\t作为struct sigevent的sigev\u notify\u attributes成员。这样,您就可以使用pthread_attr_setschedpolicy等设置的属性初始化线程。您应该使用此新信息更新/更改您的问题。请参阅nos的评论,以了解正确的方法。请注意,我的回答和对“实现细节”的引用都假设您希望编写一个可移植程序,该程序可以在其他操作系统和未来的Linux版本上工作,而不仅仅是具有当前实现细节的当前版本。谢谢您的回复!事实上,我已经试过了,但它在调度选项中不起作用。它从主进程继承sched选项。我要找的是获取为timer_create()创建的线程的ID。
SLGTRACER_DEFINE(testMain, "testMain");
timer_t audit_timer1;
void timeoutHandler(sigval_t info)
{
slgInfo(testMain, "timeoutHandler invoked");
slgInfo(testMain, "gettid() = %lu TESTMAIN", syscall(SYS_gettid));
slgInfo(testMain, "getpid() = %d TESTMAIN", getpid());
}
int main(void)
{
slgInfo(testMain, "testMain Invoked");
struct sigevent evp1;
evp1.sigev_notify = SIGEV_THREAD;
evp1.sigev_value.sival_ptr = &audit_timer1;
evp1.sigev_notify_function = timeoutHandler;
evp1.sigev_notify_attributes = NULL;
const int ERROR_BUFFER_SIZE = 50;
slgInfo(testMain, "Before FIRST timer_create");
sleep(30);
// Create timer thread
if (timer_create(CLOCK_REALTIME, &evp1, &audit_timer1) != 0)
{
// Character buffer for storing error message.
char errBuff[ERROR_BUFFER_SIZE];
memset(errBuff, 0, ERROR_BUFFER_SIZE);
slgError(testMain,
"timer_start create failed. Error = %s",
strerror_r(errno, errBuff, ERROR_BUFFER_SIZE));
timer_delete(audit_timer1);
bzero(&audit_timer1, sizeof(audit_timer1));
}
slgInfo(testMain, "After FIRST timer_create");
sleep(30);
return 0;
}
bash-3.1# ps -eL|grep testM
16651 16651 pts/0 00:00:00 testMain
16651 18018 pts/0 00:00:00 testMain
child thread with ID created by timer_create() = 18018