2016-10-09 32 views
6

को देखते हुए निम्नलिखित कार्यक्रम चलाने पर एक और टाइमर जोड़ने -पहले से ही पाश

#include <iostream> 
#include <uv.h> 

int main() 
{ 
    uv_loop_t loop; 
    uv_loop_init(&loop); 

    std::cout << "Libuv version: " << UV_VERSION_MAJOR << "." 
       << UV_VERSION_MINOR << std::endl; 

    int r = 0; 

    uv_timer_t t1_handle; 
    r = uv_timer_init(&loop, &t1_handle); 
    uv_timer_start(&t1_handle, 
     [](uv_timer_t *t) { std::cout << "Timer1 called\n"; }, 0, 2000); 

    uv_run(&loop, UV_RUN_DEFAULT); 

    // second timer 
    uv_timer_t t2_handle; 
    r = uv_timer_init(&loop, &t2_handle); 
    uv_timer_start(&t2_handle, 
     [](uv_timer_t *t) { std::cout << "Timer2 called\n"; }, 0, 1000); 

    uv_loop_close(&loop); 
} 

सेकंड टाइमर संभाल, पाश पर चलाया जाता है कभी नहीं के बाद से पाश पहले से ही चल रहा है, और "Timer2 बुलाया" कभी नहीं छपा है।

.... 
uv_run(&loop, UV_RUN_DEFAULT); 

// some work 

uv_stop(&loop); 
// now add second timer 
uv_run(&loop, UV_RUN_DEFAULT); // run again 
.... 

लेकिन यह फिर से काम नहीं किया, शायद इसलिए है क्योंकि बाद में लाइनों निष्पादित नहीं किया जाएगा के बाद 1 पाश एक दोहरा के साथ चल रहा शुरू होता है - तो मैं इसे चलाने के लिए और फिर सेकंड टाइमर जोड़ने के बाद अस्थायी रूप से पाश को रोकने की कोशिश की टाइमर। तो मैं पहले से ही uvloop चलाने के लिए एक नया टाइमर हैंडल कैसे जोड़ना चाहिए?

उत्तर

1

आप सही हैं कि एक नया हैंडल पंजीकृत करने से पहले लूप को रोका जाना चाहिए। के बाद uv_stop फ़ंक्शन को कॉल करके इसे हासिल नहीं किया जा सकता है, क्योंकि uv_run को पहले वापस लौटने की आवश्यकता है। इसे एक हैंडल कॉलबैक का उपयोग करके इसे रोककर उदाहरण के लिए हासिल किया जा सकता है। मौजूदा टाइमर 1 हैंडल का उपयोग करके यह कैसे किया जा सकता है इसका काफी मूर्ख उदाहरण है। यह लूप को पहले रन पर एक बार रोकता है।

#include <iostream> 
#include <uv.h> 

int main() { 
    uv_loop_t loop; 
    uv_loop_init(&loop); 

    std::cout << "Libuv version: " << UV_VERSION_MAJOR << "." << UV_VERSION_MINOR 
      << std::endl; 

    int r = 0; 

    uv_timer_t t1_handle; 
    r = uv_timer_init(&loop, &t1_handle); 
    *(bool *)t1_handle.data = true; // need to stop the loop 
    uv_timer_start(&t1_handle, 
       [](uv_timer_t *t) { 
        std::cout << "Timer1 called\n"; 
        bool to_stop = *(bool *)t->data; 
        if (to_stop) { 
        std::cout << "Stopping loop and resetting the flag\n"; 
        uv_stop(t->loop); 
        *(bool *)t->data = false; // do not stop the loop again 
        } 
       }, 
       0, 2000); 
    uv_run(&loop, UV_RUN_DEFAULT); 
    std::cout << "After uv_run\n"; 

    // second timer 
    uv_timer_t t2_handle; 
    r = uv_timer_init(&loop, &t2_handle); 
    uv_timer_start(&t2_handle, 
       [](uv_timer_t *t) { std::cout << "Timer2 called\n"; }, 0, 
       1000); 
    std::cout << "Start loop again\n"; 
    uv_run(&loop, UV_RUN_DEFAULT); 

    uv_loop_close(&loop); 
} 

तो उत्पादन

Libuv version: 1.9 
Timer1 called 
Stopping loop and resetting the flag 
After uv_run 
Start loop again 
Timer2 called 
Timer2 called 
Timer1 called 
Timer2 called 
Timer2 called 
Timer1 called 
+0

शांत है, इसलिए हम पाश में हेरफेर कर सकते हैं अंदर यह कॉलबैक कार्यों है, बस यकीन है कि हम यह पहली बार बंद कर दिया है बना रही है। –

संबंधित मुद्दे