本文介绍了计时器和 pthreads (POSIX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pthread 在我的应用程序上安排一些任务.我从同一个应用程序的旧 C 版本中复制了代码,它运行良好.现在我正在使用 C++ 进行编码,但它不再起作用(基本上它不会触发执行给定函数的 sigevent).即使我使用 timer_gettime,所有创建和启动函数都以 rc 0 退出.

I'm using a pthread to schedule some tasks on my application.I copied the code from my old C version of the same application and it worked perfectly.Now I'm coding with C++ and it doesn't work anymore (basically it doesn't trigger the sigevent executing the given function).All creation and starting functions exit with rc 0, even when i use the timer_gettime.

我简化了很多代码以缩小问题的范围,但我还没有找到:

I simplified a lot the code to narrow down the issue but I could't find it yet:

#include <signal.h>
#include <time.h>

int main (void)

{
char Mytimer[5] = "myt";
timer_t Ti_coarse;
Tcreate(Mytimer, &Ti_coarse, 1000, 1000);
while (1)
{

}
return 0;

static int Tcreate( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
   struct sigevent         te;
   struct itimerspec       its;
   struct sigaction        sa;
   int                     sigNo = SIGRTMIN;

   sa.sa_flags = SA_SIGINFO;
   sa.sa_sigaction = app;
   sigemptyset(&sa.sa_mask);
   if (sigaction(sigNo, &sa, NULL) == -1)
   {
       perror("sigaction");
   }

   /* Set and enable alarm */
   te.sigev_notify = SIGEV_SIGNAL;
   te.sigev_signo = sigNo;
   te.sigev_value.sival_ptr = timerID;
   timer_create(CLOCK_REALTIME, &te, timerID);

   its.it_interval.tv_sec = 0;
   its.it_interval.tv_nsec = intervalMS * 1000000;
   its.it_value.tv_sec = 0;
   its.it_value.tv_nsec = expireMS * 1000000;
   timer_settime(*timerID, 0, &its, NULL);

   return 1;
}

static void app(int sig, siginfo_t *si, void *uc)
{
    //do nothing
}

我希望每秒都能看到这是计时器",但我没有得到任何输出.如果我用 printf("%d", its.it_value) 检查 5 分钟时钟的到期时间,无论过去多久,我总是得到 299.

I expected to see the "THIS IS THE TIMERS" every second, but I don't get any output.If I check the expiration time of a 5 mins clock with printf("%d", its.it_value) I always get 299, no matter how much time passed.

你能帮我找出问题吗?

推荐答案

首先,您没有提供任何人都可以用来重现您的问题的完整示例.请参阅如何创建最小、完整且可验证的示例.

First, you have not provided a full example that anyone can use to reproduce your problem. See How to create a Minimal, Complete, and Verifiable example.

但是您发布的代码确实存在一个严重问题.您不能在信号处理程序中安全地使用 cout.您的信号处理程序可能会导致未定义的行为:

But the code you posted does have one serious problem. You can't safely use cout in a signal handler. Your signal handler can result in undefined behavior:

static void app(int sig, siginfo_t *si, void *uc)
{
    cout << "THIS IS THE TIMER" << endl;
}

根据 POSIX 标准:

下表定义了一组函数异步信号安全.因此,应用程序可以调用它们,而无需限制,来自信号捕捉功能....

(在信号处理程序中可以安全调用的大型函数表)

(large table of functions that are safe to call in a signal handler)

实现可能使其他接口异步信号安全.在里面信号的存在,此卷定义的所有功能POSIX.1-2008 在调用或中断时应按照定义的方式运行通过信号捕捉功能,除了当一个信号中断不安全的功能或等效功能(例如处理相当于从初始调用返回后执行的 exit()main()) 和信号捕捉函数调用了一个不安全的函数,行为未定义.

Implementations may make other interfaces async-signal-safe. In the presence of signals, all functions defined by this volume of POSIX.1-2008 shall behave as defined when called from or interrupted by a signal-catching function, with the exception that when a signal interrupts an unsafe function or equivalent (such as the processing equivalent to exit() performed after a return from the initial call to main()) and the signal-catching function calls an unsafe function, the behavior is undefined.

使用 cout 不在异步信号安全函数表中,因此从信号处理程序调用它会导致未定义的行为.

Using cout is not in the table of async-signal-safe functions, so calling it from a signal handler can cause undefined behavior.

这篇关于计时器和 pthreads (POSIX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:37