1

libuv入门

 2 years ago
source link: https://forrestsu.github.io/posts/library/libuv%E5%85%A5%E9%97%A8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

libuv入门

2017年4月19日
| 字数 912
| 阅读 19

libuv 定时器timer 使用: g++ -o sunquan main.cpp -luv 执行 ./sunquan 可以看到每隔1秒打印一次 count的值。

uv_timer_start(&timer, timer_cb, timeout, repeat); 其中timeout是首次触发等待的时间毫秒值,之后每隔repeat毫秒触发一次,如果repeat=0表示首次触发之后不再触发。

//main.cpp
#include <cstdlib>
#include <uv.h>
#include <assert.h>
#include <time.h>
#include <iostream>
using namespace std;

void timer_cb(uv_timer_t *handlei)
{
   static int count=1;
   cout<<"count=="<<count++<<endl;
}
int main()
{
    int r;
    uv_timer_t timer;
    r = uv_timer_init(uv_default_loop(),&timer);
    assert(r==0);
    // 0
    cout<<uv_is_active((uv_handle_t*) &timer)<<endl;
    // 0
    cout<< uv_is_closing((uv_handle_t*) &timer)<<endl;
    cout<<"start "<<time(NULL)<<endl;

    r = uv_timer_start(&timer, timer_cb, 5000, 1000);
    r = uv_run(uv_default_loop(),UV_RUN_DEFAULT);
    cout<<"r="<<r<<endl;
    return 0;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK