C++, time, ctime



  • How do you get an increase of one second? And is it a good decision with the cycle?

    k=k+1;
    sleep('1000');
    k=k+1; 
    


  • If you go to the forehead, create the flow:

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

    void timer_start(std::function<void(void)> func, unsigned int interval){
    std::thread(func, interval {
    while (true){
    func();
    std::this_thread::sleep_for(std::chrono::milliseconds(interval));
    }
    }).detach();
    }

    void do_something(){
    // здесь увеличивайте значение
    }

    int main() {
    timer_start(do_something, 1000);

    while(true);
    

    }

    Your code, if it's mostly flowing, will block everything.

    You can use it. libevent:

    #include <stdio.h>
    #include <sys/time.h>
    #include <event.h>

    void inc_value(int fd, short event, void *arg){
    // здесь увеличиваем переменную
    }

    int main(int argc, const char* argv[]){
    struct event ev;
    struct timeval tv;

    tv.tv_sec = 1;
    tv.tv_usec = 0;

    event_init();
    evtimer_set(&ev, inc_value, NULL);
    evtimer_add(&ev, &tv);
    event_dispatch();

    return 0;
    }

    If you use the boost, it's there. http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tuttimer2.html ♪

    If it's okay for you to bleed the main flow,

    int k = 0;
    int limit = 5;
    while(1) {
    sleep(1000);
    ++k;
    if( k >= limit)
    break;
    }

    call_me_after_all();


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2