Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 操作系统回调如何工作_C++_C_Unix_Posix - Fatal编程技术网

C++ 操作系统回调如何工作

C++ 操作系统回调如何工作,c++,c,unix,posix,C++,C,Unix,Posix,后续问题: 如链接问题中所述,我们有一个API,它使用轮询select()的事件外观来处理用户定义的回调 我有一个类使用这样的: class example{ public: example(){ Timer* theTimer1 = Timer::Event::create(timeInterval,&example::FunctionName); Timer* theTimer2 = Timer::Event::create(timeI

后续问题:

如链接问题中所述,我们有一个API,它使用轮询select()的事件外观来处理用户定义的回调

我有一个类使用这样的:

class example{
 public:
    example(){
        Timer* theTimer1 =  Timer::Event::create(timeInterval,&example::FunctionName);
        Timer* theTimer2 =  Timer::Event::create(timeInterval,&example::FunctionName);
        start();
       cout<<pthread_self()<<endl;
    }
  private:
     void start(){
        while(true){
           if(condition)
              FunctionName();
           sleep(1);
        }
     }
     void FunctionName(){
         cout<<pthread_self()<<endl;
         //Do stuff
     }
};
类示例{
公众:
示例(){
Timer*theTimer1=Timer::Event::create(timeInterval,&示例::FunctionName);
Timer*theTimer2=Timer::Event::create(timeInterval,&示例::FunctionName);
start();

cout编写的代码不会编译,更不用说运行了。示例::FunctionName需要是静态的,并且需要使用对象引用作为回调函数


如果计时器在不同的线程中运行,则此函数可能由三个不同的线程调用。

真正的答案将取决于计时器的实现,但如果您从同一线程运行回调,则最有可能使用or。无论哪种方式,都不涉及


对于信号和posix计时器,您可以从信号处理程序安全地执行的操作非常少。只允许使用特定的信号安全调用,例如and(NOTfread()和fwrite(),甚至new和cout)。通常,您要做的是write()在一个OR中,然后在另一个线程中,或者运行主事件循环(Stand()),注意这个通知并处理它。这允许您以安全的方式处理信号。

应该标记为C而不是C++ +汤姆,代码示例显然是C++…您是正确的。事件和计时器。>。<这似乎是检查循环中的事件时间!(单独的线程)顺便说一句,使用信号的API真的很难使用。鼓励使用信号的API的人将这些回调作为线程中的常规函数运行,这样它们就只需要锁定:)现在我只是用自己的事件循环来绕过这一点。我知道这很讨厌,但至少我可以让它们成为线程安全的。。。