- libuv的使用:
- //(1)创建 uv_async_t async;
- // uv_loop_t* loop = uv_default_loop();
- //(2)开启主线程 uv_async_init(loop, &async, uv_asynccb);---》(void uv_asynccb(uv_async_t* handle))
- //(3)激活持有async的队列 uv_async_send(&async);
- //(4)循环队列 uv_run(loop, UV_RUN_DEFAULT);
- //(5)清楚指针 uv_loop_close(uv_default_loop());
- //线程使用:
- //(1)创建句柄 uv_thread_t thread;
- //(2)创建线程 uv_thread_create(&thread, sub_thread, (void *)&async);
- //(3)回收 uv_thread_join(&thread)
- //工作队列使用
- //(1)创建句柄 uv_work_t req;
- //(2)创建工作 uv_queue_work(loop, &req, callback, finishcallback)
- //定时器使用
- //(1)创建句柄 uv_timer_t m_timer;
- //(2)创建定时器回掉 uv_timer_start(&m_timer, handle, timeout, repalce);
- //(3)回收 uv_timer_stop(&m_timer)
- //DNS查询
- //(1)创建句柄 uv_getaddrinfo_t resolver
- //(2)解析 int r = uv_getaddrinfo(loop, &resolver, on_resolved, "irc.freenode.net", "6667", struct addrinfo*);
- //回掉void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)--》你可以在struct addrinfo(s) 结构的链表中任取一个 IP
- //(3)返回值 返回非0解析出错,回掉不会调用,所有参数回值返回后释放
复制代码 直接从代码的角度讲解
(1)uv_async_init队列进程
- void uv_asynccb(uv_async_t* handle)
- {
- printf("async_cb called!\n");
- uv_thread_t id = uv_thread_self();
- printf("thread id:%lu.\n", id);
- uv_close((uv_handle_t*)handle, NULL);//从队列中删除本任务,程序会直接退出
- //uv_async_send(handle);//若不删除任务,再次激活程序会陷入循环
- }
- int main(int argc, char* argv[])
- {
- uv_async_t async;//创建句柄
- uv_loop_t* loop = uv_default_loop();
- uv_thread_t id = uv_thread_self();
- printf("thread id:%lu.\n", id);//得到自身TID
- uv_async_init(loop, &async, uv_asynccb);//投入队列
复制代码 执行结果:
可见uv_async_init并不会创建线程,程序还是同一个线程
(2)线程使用:
- void sub_thread(void* arg)
- {
- uv_thread_t id = uv_thread_self();
- printf("sub thread id:%lu.\n", id);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- uv_thread_t thread;
- uv_loop_t* loop = uv_default_loop();
-
- uv_thread_t id = uv_thread_self();
- printf("thread id:%lu.\n", id);
- //创建子线程(子线程不等于队列,线程thread_t是不一样的)
- uv_thread_create(&thread, sub_thread, NULL);
- uv_run(loop, UV_RUN_DEFAULT);
- uv_loop_close(uv_default_loop());
- return 0;
- }
复制代码 执行结果
- thread id:0
- sub thread id:456
复制代码 tid不同,创建了线程,且程序直接返回
(3)定时器
- int _tmain(int argc, _TCHAR* argv[])
- {
- uv_timer_t m_timer;
-
- uv_thread_t id = uv_thread_self();
- printf("thread id:%lu.\n", id);
复制代码 执行结果
- thread id:0
- thread id:453
- thread id:453
- thread id:453
- 。。。
复制代码 定时器也为线程运行,代码中
- [](uv_timer_t* handle)
- {
- printf("======TIME========!\n");
- uv_thread_t id = uv_thread_self();
- printf("thread id:%lu.\n", id);
- }
复制代码 上述写法是在调用时对函数进行实现
(4)工作队列
- int _tmain(int argc, _TCHAR* argv[])
- {
- uv_async_t async;
- uv_work_t req;
- uv_loop_t* loop = uv_default_loop();
-
- uv_thread_t id = uv_thread_self();
- printf("thread id:%lu.\n", id);
- uv_queue_work(loop, &req, [](uv_work_t* handle)
- {
- printf("async_cb called2!\n");
- uv_thread_t id = uv_thread_self();
- printf("thread id:%lu.\n", id);
- }
- , NULL);
- uv_run(loop, UV_RUN_DEFAULT);
- uv_loop_close(uv_default_loop());
- return 0;
- }
复制代码 执行结果同定时器
DNS解析
- #include <iostream>
- #include <string>
- #include <uv.h>
- using namespace std;
- //回掉函数
- void callback(uv_getaddrinfo_t* req, int status, struct addrinfo* res){
- cout << "status: " << status << endl;
- while (res)
- {
- struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr;//从res得到解析结果
- cout << "addr: " << inet_ntoa(addr->sin_addr) << " : " << ntohs(addr->sin_port) << endl;//打印端口和IP
- res= res->ai_next;
- }
- uv_freeaddrinfo(res);//必须释放
- }
- int main(int argc, char* argv[])
- {
- uv_loop_t* loop = uv_default_loop();
- uv_getaddrinfo_t uv_req;//创建句柄
- int r = uv_getaddrinfo(uv_default_loop(), &uv_req, callback, "www.baidu.com", NULL, NULL);//解析DNS
- if (r) {
- cout << "dns:error" << endl;
- return 1;
- }
- uv_run(loop, UV_RUN_DEFAULT);
- uv_loop_close(uv_default_loop());
- return 0;
- }
复制代码 执行结果
- status: 0
- addr: 180.97.33.108 : 0
- addr: 180.97.33.108 : 0
- addr: 180.97.33.108 : 0
- addr: 180.97.33.107 : 0
- addr: 180.97.33.107 : 0
- addr: 180.97.33.107 : 0
复制代码
|