2010-05-12 95 views
3

我试图建立在C++ Feed阅读器,所以我需要的程序检查间歇新的饲料。但是,用户仍然需要能够与程序进行交互,所以我似乎不断寻找,让系统等待的建议不适用于我。任何人都可以提出更好的解决方案,比如说在后台运行的计时器或者其他什么东西?运行在C每x秒的函数++

感谢, 查尔斯

+0

? – Jacob 2010-05-12 00:15:27

+0

我使用gcc 4.4.1 – wyatt 2010-05-12 00:16:41

+3

单独的后台计时器不会削减它;完全有可能在尝试读取Feed时,由于某种原因,您的程序在某些网络调用中会超时而无法控制,这意味着您的程序也会在这种情况下冻结。为了妥善解决这两个问题,你需要输入多线程的神奇世界... – Jon 2010-05-12 00:18:27

回答

2

您可以创建睡在特定时间段的线程。这与操作系统无关。或者,如果您在Windows中编程,则可以设置定时器定期发送超时事件。定时器的使用取决于您的部署平台。

+0

谢谢,我会看看那个。我知道这是非常主观的,但是如果只是暂停一个线程,看起来有点混乱,或者这只是它的完成方式? – wyatt 2010-05-12 00:19:17

+0

线程暂停其实真的很好 - 操作系统可以把自己说:“好吧,他不会使用,在未来x秒,我可以安排在此期间一些其他的任务”。另一种方法是建立一个不断运行的循环,并不断检查“时间到了吗?”在这种情况下,操作系统将继续为“暂停”线程分配时间;因此效率更低。 – Smashery 2010-05-12 00:22:23

+2

请注意,如果您只有1秒的固定暂停时间,并且工作需要半秒钟才能完成,那么您最终每1.5秒钟运行一次。您可能想要考虑工作开展的时间。 – AshleysBrain 2010-05-12 09:32:14

2

你需要使用线程。在后台执行一个线程执行定时器,并在前台执行一个与用户交互的线程。然后在后台线程可以通过调用你的特定函数修改线程之间有一些共享内存区域;并且可以查看前景。也许考虑升压线程库:http://www.boost.org/doc/libs/1_43_0/doc/html/thread.html

1

使UI和饲料读者单独的线程。如果用户做了需要立即更新Feed的内容,请中断Feed线程。

0

对我来说,这听起来像的观测器设计模式的候选者:

http://en.wikipedia.org/wiki/Observer_pattern#C.2B.2B

产生一个线程,这将intermittantly检查饲料,然后调用handleEvent方法在你的主类/混凝土观测器设计类。这样你的主班不会处于等待状态。

1

可以使用SIGALRM得到所中断每隔n秒。这不需要单独的线程。你的主线程会进入一个信号处理程序。

void sigtime(int signo) 
{ 
    signal(SIGALRM, sigtime); 
} 
.... 
signal(SIGALRM, sigtime); 
itimerval itm; 
itm.it_interval.tv_sec=0; 
itm.it_value.tv_sec = 0; 
itm.it_interval.tv_usec = 200000; 
itm.it_value.tv_usec = 200000; 
setitimer(ITIMER_REAL,&itm,0); 

这当然你承担的东西类Unix

+0

+1和http://www.opengroup.org/onlinepubs/009695399/functions/getitimer.html另请参阅报警http://www.opengroup.org/onlinepubs/009695399/functions/alarm.html,其中放弃了结构简单的积分秒 – Potatoswatter 2010-05-12 01:22:14

0

我也想让它执行一个功能每隔x分钟或小时的节目,我发现有很多的例子,但对于做它,它是必要的,包括和下载库,对我来说是不舒服,我做了我自己,不是很逻辑)),但它的工作原理,可以是您使用的编译器看到它下面

#include <stdlib.h> 
#include <iostream> 
#include <time.h> 
using namespace std; 

struct tm *addtime(struct tm *tm2) 
{ 
     time_t t = time(0); // get time now 
    struct tm * tm1 = localtime(& t); 
    cout << " Time begin : " << tm1->tm_hour << " : " << tm1->tm_min <<endl; 
    struct tm * aux = (struct tm*)malloc(sizeof (struct tm)); 

    aux->tm_sec = tm1->tm_sec + tm2->tm_sec; 
    aux->tm_min = tm1->tm_min + tm2->tm_min + (aux->tm_sec/60) ; 
    aux->tm_hour = tm1->tm_hour + tm2->tm_hour + (aux->tm_min/60); 
    aux->tm_min %= 60; 
    aux->tm_sec %= 60; 
    return (aux); 

} 
bool verif_time(struct tm *tm1) 
{ 

    time_t t = time(0); // get time now 
    struct tm * now = localtime(& t); 
    if (tm1->tm_hour == now->tm_hour && tm1->tm_min == now->tm_min) 
     return true; 
    else 
     return false; 

} 

    int main() 
    { 
    struct tm * after = (struct tm*)malloc(sizeof (struct tm)); 

    after->tm_sec = 0; // here you can modify difference between now time and time when you want to execute function 
    after->tm_min = 1; 
    after->tm_hour = 0; 

    after = addtime(after); 
    cout << " After time" << after->tm_hour << ':' << after->tm_min<<endl; 
    while (true) 
    { 
     if (verif_time(after)) 
     { 
      cout << "Hello " << after->tm_hour << " : " << after->tm_min<<endl; //here you can include your function 

      after->tm_sec = 0; 
      after->tm_min = 1; 
      after->tm_hour = 0; // here also 

      after = addtime(after); 
      cout << " After time" << after->tm_hour << ':' << after->tm_min<<endl; 


     } 
    } 

    }