libuv_async

个人理解:多线程的数据中转工具。

展示一个demo,线程1给线程2传递一个值=123。

#include <stdio.h>
#include <stdlib.h>

#include <uv.h>
#pragma comment(lib,"uv.lib")

uv_loop_t *loop;
uv_async_t async;

void close_cb(uv_handle_t* handle) {
	printf("close the handle!\n");
}

void callback_async_init(uv_async_t* handle, int status) {
	uv_close((uv_handle_t*)&async, close_cb);	//如果async没有关闭,消息队列是会阻塞的
}
void callback_thread1(void * args) {
	int num = 123;
	async.data = num;
	uv_async_send(&async);
}
void callback_thread2(void * args) {
	int num = (int)async.data;
	printf("%d\n", num);
}

int main() {
	loop = uv_default_loop();
	uv_async_init(loop, &async, callback_async_init);
	uv_thread_t th1, th2;
	uv_thread_create(&th1, callback_thread1, NULL);
	uv_thread_create(&th2, callback_thread2, NULL);
	uv_thread_join(&th1);	//等待子线程完成
	uv_thread_join(&th2);	//等待子线程完成

	uv_run(loop, UV_RUN_DEFAULT);
	getchar();
	return 0;

}

posted @ 2023-03-09 09:06  对CSDN使用炎拳吧  阅读(26)  评论(0编辑  收藏  举报