Closed BedRockJie closed 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.
如果存在多进程更新写入同一文件,写函数是否会存在问题?
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.
write_dat_to_file 函数在写时没有对文件进行加锁
是否考虑加锁以支持并发场景?