2010-05-12 68 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; 


     } 
    } 

    }