CnxSharedMutex struct
#include <include/Cnx/sync/SharedMutex.h>
CnxSharedMutex
is the generic (it's not timed or recursive) reader-writer mutex provided by Cnx. It's directly comparable to C++'s std::shared_mutex
and is suitable for any situation where a reader-writer mutex is necessary.
Example:
// MyThing.h #include <Cnx/sync/SharedMutex.h> static MyType my_very_important_thing; static CnxSharedMutex* my_thing_mutex; void init_my_thing(void); void update_my_thing(u64 value); u64 get_value_from_my_thing(void); // MyThing.c #include <Cnx/Allocators.h> #include "MyThing.h" void init_my_thing(void) { if(my_thing_mutex == nullptr) { my_thing_mutex = cnx_allocator_allocate_t(CnxSharedMutex, DEFAULT_ALLOCATOR); *my_thing_mutex = cnx_shared_mutex_new(); cnx_shared_mutex_lock(my_thing_mutex); my_very_important_thing = { // important intialization }; cnx_shared_mutex_unlock(my_thing_mutex); } } // only one thread at a time can `update_my_thing`, because it uses exclusive locking void update_my_thing(u64 value) { cnx_shared_mutex_lock(my_thing_mutex); my_very_important_thing.value = value; cnx_shared_mutex_unlock(my_thing_mutex); } // any number of threads can `get_value_from_my_thing` without blocking eachother, // because it uses shared locking. u64 get_value_from_my_thing(void) { cnx_shared_mutex_lock_shared(my_thing_mutex); let val = my_very_important_thing.value; cnx_shared_mutex_unlock_shared(my_thing_mutex); return val; } // do some compute intensive task... // update the value update_my_thing(new_value); my_val = get_value_from_my_thing(); // do something with my_val let_mut newval = get_value_from_my_thing(); while(newval == my_val) { cnx_this_thread_sleep_for(cnx_milliseconds(100)); newval = get_value_from_my_thing(); } my_val = newval; // do something with new value