回拨函数问题

时间:2015-07-21 13:18:25

标签: c++ function callback

我正在尝试回调功能,我看不出这里出了什么问题。

func1接受两个函数指针,我将指针传递给func2和func3。 func2应该在打印到cout之前等待5秒,func3应该在打印到cout之前等待12秒。我自己实现的wait()函数,但不包括在这里。

当程序运行时,5秒后,func2和func3同时打印出来。我本来希望等待5秒钟才能打印func2,然后再等待12秒钟才能打开func3,但事实并非如此。

任何人都可以看到这里有什么问题吗?提前谢谢了。

#define ONE_SEC 1000000000/3

void wait(int);
void func1(void(*)(), void(*)());
void func2();
void func3();

int main()
{

    std::cout<<"In Main Program!!"<<std::endl;

    void (*funcPtr2)();
    void (*funcPtr3)();

    funcPtr2 = func2;
    funcPtr3 = func3;


    func1(funcPtr2, funcPtr3);

    return 0;
}

void func1(void (*fptr2)(), void (*fptr3)())
{
    std::cout<<"In function one!!"<<std::endl;
    (*fptr2)();
    (*fptr3)();
}

void func2()
{
     wait(5);
     std::cout<<"In function 2 - callback function!!"<<std::endl;

}

void func3()
{
    wait(20);
    std::cout<<"In function 3 - callback function!!"<<std::endl;
}

void wait (int secs)
{
    long j;
    long i;

    for (i = 0; i < secs * (ONE_SEC); i++)
    {
        j++;
    }

}

3 个答案:

答案 0 :(得分:1)

由于你重新发明轮子不起作用,试试这个:

#include <iostream>
#include <thread>
#include <chrono>

int main()
{
    std::cout << "yo" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(10));
    std::cout << "whats" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "up" << std::endl;

    std::cin.get();
    return 0;
}

如果你不能和你在窗户上,那就去睡觉吧(秒)或者在unix上睡觉吧

答案 1 :(得分:1)

试试这个

#include <iostream>
#include <stdlib.h>
#include <time.h>
 void wait ( int seconds )
 {
    clock_t endwait;
    endwait = clock () + seconds * CLOCKS_PER_SEC ;
    while (clock() < endwait) {}
 }

 void func1(void(*)(), void(*)());
 void func2();
 void func3();

 int main()
 {

    std::cout<<"In Main Program!!"<<std::endl;

    void (*funcPtr2)();
    void (*funcPtr3)();

    funcPtr2 = func2;
    funcPtr3 = func3;


    func1(funcPtr2, funcPtr3);

    return 0;
 }

 void func1(void (*fptr2)(), void (*fptr3)())
 {
    std::cout<<"In function one!!"<<std::endl;
    (*fptr2)();
    (*fptr3)();
 }

 void func2()
 {
    wait(5);
    std::cout<<"In function 2 - callback function!!"<<std::endl;

 }

 void func3()
 {
    wait(20);
    std::cout<<"In function 3 - callback function!!"<<std::endl;
 }

答案 2 :(得分:0)

假设您必须实现自己的wait函数,而不是调用一个更合理的this_thread::sleep_for或检查clock()实现,那么下面的代码(您的代码)已经未定义的行为,因为您从不初始化j

void wait (int secs)
{
    long j;
    long i;

    for (i = 0; i < secs * (ONE_SEC); i++)
    {
        j++;
    }
}

原始类型(intlongboolchar)需要默认初始化,因为它们没有构造函数(它们不是实际的类) C ++)。 (当变量存在于文件/命名空间范围时,它将被零初始化,但这种情况很少发生)。因此,只需将j设置为0即可消除未定义的行为:

void wait (int secs)
{
    long j = 0;
    long i;

    for (i = 0; i < secs * (ONE_SEC); i++)
    {
        j++;
    }
}

但它有点多余不是吗?以下不一样吗?

void wait (int secs)
{
    for (size_t i = 0; i < secs * (ONE_SEC); i++)
    {}
}

但是,问题是,除非ONE_SECsecs在编译时或运行时确定,具体取决于您的系统,否则几乎不能保证您的程序会等待指定的时间。随着硬件的改进,您将等待的时间越来越少。

最终,我会说要使用现在标准的this_thread::sleep_for() another user posted earlier

#include<thread>
#include<chrono>
// ...
void wait (int secs)
{
    std::this_thread::sleep_for(std::chrono::seconds(secs));
}
相关问题