ibireme / yyjson

The fastest JSON library in C
https://ibireme.github.io/yyjson/doc/doxygen/html/
MIT License
2.98k stars 262 forks source link

Does yyjson support concurrency #129

Closed BedRockJie closed 1 year ago

BedRockJie commented 1 year ago

write_dat_to_file 函数在写时没有对文件进行加锁

static bool write_dat_to_file(const char *path, u8 *dat, usize len,
                              yyjson_write_err *err) {

#define return_err(_code, _msg) do { \
    err->msg = _msg; \
    err->code = YYJSON_WRITE_ERROR_##_code; \
    if (file) fclose(file); \
    return false; \
} while (false)

    FILE *file = fopen_writeonly(path);
    if (file == NULL) {
        return_err(FILE_OPEN, "file opening failed");
    }
    if (fwrite(dat, len, 1, file) != 1) {
        return_err(FILE_WRITE, "file writing failed");
    }
    if (fclose(file) != 0) {
        file = NULL;
        return_err(FILE_WRITE, "file closing failed");
    }
    return true;

#undef return_err
}

是否考虑加锁以支持并发场景?

TkTech commented 1 year ago

The documentation is pretty clear:

Thread Safety

The library does not use global variables. Therefore, if you can ensure that the input parameters of a function are thread-safe, then the function calls are also thread-safe.

In general, yyjson_doc and yyjson_val are immutable and thread-safe, while yyjson_mut_doc and yyjson_mut_val are mutable and not thread-safe.

BedRockJie commented 1 year ago

如果存在多进程更新写入同一文件,写函数是否会存在问题?

ibireme commented 1 year ago

It is indeed unsafe to write to the same file concurrently. This document also mentions it: https://github.com/ibireme/yyjson/blob/master/src/yyjson.h#L1063.

This function is thread-safe when:
 1. The file is not accessed by other threads.
 2. The `alc` is thread-safe or NULL.

Windows LockFileEx() or Unix flock() may ensure the safety of file writing with multi-threading, but these functions are not ANSI C-compliant, so yyjson cannot use them. You can use the function yyjson_write_fp and flock()/LockFileEx() to wrap it yourself to meet your needs.