2010-08-22 29 views
11

Tôi mong đợi mã dưới đây để in Xin chào, thế giới! cứ 5 giây một lần, nhưng những gì xảy ra là chương trình tạm dừng trong 5 giây và sau đó in đi lặp lại thông báo mà không bị tạm dừng sau đó. Tôi đang thiếu gì?boost asio deadline_timer

#include <iostream> 
#include <boost/asio.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

using namespace boost::asio; 
using namespace std; 

io_service io; 

void print(const boost::system::error_code& /*e*/) 
{ 
    cout << "Hello, world!\n"; 
    deadline_timer t(io, boost::posix_time::seconds(5)); 
    t.async_wait(print); 
} 


int main() 
{ 

    deadline_timer t(io, boost::posix_time::seconds(5)); 
    t.async_wait(print); 

    io.run(); 

    return 0; 
} 

chỉnh sửa để thêm mã hoạt động bên dưới. cảm ơn các bạn.

#include <iostream> 
#include <boost/bind.hpp> 
#include <boost/thread.hpp> 
#include <boost/asio.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

using namespace boost::asio; 
using namespace std; 

class Deadline { 
public: 
    Deadline(deadline_timer &timer) : t(timer) { 
     wait(); 
    } 

    void timeout(const boost::system::error_code &e) { 
     if (e) 
      return; 
     cout << "tick" << endl; 
     wait(); 
    } 

    void cancel() { 
     t.cancel(); 
    } 


private: 
    void wait() { 
     t.expires_from_now(boost::posix_time::seconds(5)); 
     t.async_wait(boost::bind(&Deadline::timeout, this, boost::asio::placeholders::error)); 
    } 

    deadline_timer &t; 
}; 


class CancelDeadline { 
public: 
    CancelDeadline(Deadline &d) :dl(d) { } 
    void operator()() { 
     string cancel; 
     cin >> cancel; 
     dl.cancel(); 
     return; 
    } 
private: 
    Deadline &dl; 
}; 



int main() 
{ 
    io_service io; 
    deadline_timer t(io); 
    Deadline d(t); 
    CancelDeadline cd(d); 
    boost::thread thr1(cd); 
    io.run(); 
    return 0; 
} 
+0

những gì 'HủyĐường dẫn 'là gì? Tôi đã nhận xét lớp này và 'CancelDeadline cd (d); boost :: thread thr1 (cd); 'và mã vẫn hoạt động. – javapowered

+0

Bạn có thể quan tâm đến câu hỏi này: http://stackoverflow.com/questions/21771639/can-i-use-a-stackful-coroutine-as-the-wait-handler-of-a-steady-timer-which -is-de – updogliu

Trả lời

23

Bạn đang tạo deadline_timer làm biến cục bộ và sau đó thoát khỏi chức năng ngay lập tức. Điều này làm cho bộ đếm thời gian hủy và hủy chính nó, và gọi hàm của bạn với một mã lỗi mà bạn bỏ qua, gây ra vòng lặp vô hạn.

Sử dụng một đối tượng bộ hẹn giờ duy nhất, được lưu trữ trong một biến thành viên hoặc toàn cầu, nên khắc phục điều này.

2

Nếu bạn nhìn vào mã lỗi, bạn sẽ nhận được lỗi hủy hoạt động.

+0

Hmm, thật thú vị và cảm ơn vì đã chỉ ra điều đó. Tại sao bạn cho rằng tôi bị lỗi do hoạt động? – shaz

+0

Kiểm tra phản hồi của interjay. Điều đó khắc phục hoàn toàn vấn đề. –

2
#include <iostream> 
#include <boost/asio.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

using namespace boost::asio; 
using namespace std; 

io_service io; 

deadline_timer t(io, boost::posix_time::seconds(5)); 

void print(const boost::system::error_code& /*e*/) 
{ 
    cout << "Hello, world!\n"; 
    t.expires_from_now(boost::posix_time::seconds(5)); 
    t.async_wait(print); 
} 


int main() 
{ 

    //deadline_timer t(io, boost::posix_time::seconds(5)); 
    t.async_wait(print); 

    io.run(); 

    return 0; 
} 
+3

Chăm sóc để thêm bất kỳ loại bình luận nào? Có câu trả lời chỉ là mã hiếm khi thực sự giúp người hỏi câu hỏi. –

Các vấn đề liên quan