symisc / unqlite

An Embedded NoSQL, Transactional Database Engine
https://unqlite.symisc.net
Other
2.11k stars 164 forks source link

Question about single process, multiple threads with unqlite_demovfs.c #126

Closed mattb351 closed 3 years ago

mattb351 commented 3 years ago

I have a single process with multiple threads. If I understand correctly, I should be able to use the unqlite_demovfs.c code to access the db safely in multiple threads? I have compiled with -DUNQLITE_ENABLE_THREADS then I call unqlite_lib_config(UNQLITE_LIB_CONFIG_THREAD_LEVEL_MULTI ); before I call unqlite_open(). After that, unqlite_lib_is_threadsafe() returns TRUE.

Would it be fair to say the unqlite already has the mutexes necessary to allow multiple thread access to the db file and I don't need to add anything to unqlite_demovfs.c?

Is unqlite-kv able to be compiled for multiple thread access? I probably don't need the jx9 stuff.

symisc commented 3 years ago

Yes, that's the right approach for multi-threaded handles. Compile the library first with UNQLITE_ENABLE_THREADS directive enabled and activate multi-threading at run-time via unqlite_lib_config(UNQLITE_LIB_CONFIG_THREAD_LEVEL_MULTI );.

Once UnQLite is compiled with multi threading support, all mutexes are enabled including the recursive ones on database handles and Virtual Machine objects and it is safe then to share the same database handle with multiple threads!

https://unqlite.org/c_api_const.html#compile_time