我是uvlib
的新手。如果想避免內部功能被阻塞,兩次調用uv_run
是否正常?如果沒有,那麼哪些工具可用,線程除外?在這裏我只是打開和關閉文件。幾次調用uv_run是否正常?
#include <uv.h>
#include <stdio.h>
#include <fcntl.h>
#include <conio.h>
#ifdef _WIN32
#include <conio.h>
#include <Windows.h>
#define Sleep(x) Sleep(x)
#else
#include <unistd.h>
#define Sleep(x) sleep(x)
#endif
uv_loop_t* loop;
uv_fs_t open_req;
uv_fs_t close_req;
void open_cb(uv_fs_t*);
void close_cb(uv_fs_t*);
const char *filename = "C:/c/somedata.txt";
int main(int argc, char **argv) {
int r;
loop = uv_loop_new();
r = uv_fs_open(loop, &open_req, filename, O_RDONLY, S_IREAD, open_cb);
if (r < 0) {
printf("Error at opening file: %s\n", uv_strerror(r));
}
printf("in main now\n");
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
return 0;
}
void open_cb(uv_fs_t* req) {
int result = req->result;
if (result < 0) {
printf("Error at opening file: %s\n", uv_strerror(result));
} else {
printf("Successfully opened file.\n");
}
uv_fs_req_cleanup(req);
uv_fs_close(loop, &close_req, result, close_cb);
uv_run(loop, UV_RUN_DEFAULT);
Sleep(5000);
printf("ok now\n");
}
void close_cb(uv_fs_t* req) {
int result = req->result;
printf("in close_cb now\n");
if (result < 0) {
printf("Error at closing file: %s\n", uv_strerror(result));
} else {
printf("Successfully closed file.\n");
}
}