简体   繁体   中英

New thread on invocation of timer_create()

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

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?

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().

I appreciate your help very much!

Code:

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM