新线程调用timer_create() [英] New thread on invocation of timer_create()

查看:244
本文介绍了新线程调用timer_create()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调查如何在调用函数timer_create()时获取线程ID。
我观察到每次timer_create()被调用,一个新的线程(主进程的子线程)被创建。我验证这与ps -eL | grep

I am investigating how to get thread ID when the function timer_create() is called. I observed that everytime the timer_create() is called, a new thread (child thread of main process) is created. I verified this with ps -eL|grep

我需要获得相同的TID(子线程ID)显示ps -eL在我的程序,使用timer_create。
从下面的代码:我如何得到TID 18018在我的程序?

I need to get the same TID (child thread ID) displayed by ps -eL inside my program which uses timer_create. From code below: How do I get the TID 18018 inside my program?

我研究了所有的职位,每一个提到一个新的线程创建调用通知函数而不调用timer_create()。

I have researched all the posts and each one mentions that a new thread is created on calling the notification function and not calling timer_create().

非常感谢您的帮助!

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


推荐答案

在超时处理程序被调用之前线程是否存在未指定。当然,符合的实现可以延迟线程的创建,直到定时器到期。看起来像pids的数字线程id的存在也是一个实现细节,你不应该使用它。

Whether a thread even exists before the timeout handler gets called is unspecified. Certainly a conformant implementation could defer creation of the thread until the timer expires. The existence of numeric thread ids that look like pids is also an implementation detail, one which you should not use.

你能解释一下你想要完成什么吗?当然有更好的方法...

Could you explain what you're trying to accomplish? Surely there is a better way...

这篇关于新线程调用timer_create()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆