CnxMutex struct
#include <include/Cnx/sync/Mutex.h>
CnxMutex
is the generic higher-level mutex type provided by Cnx. It is a simple mutex type suitable for general purpose use (ie, it is not timed, recursive, etc), and is directly comparable to C++'s std::mutex
Example:
// MyThing.h #include <Cnx/sync/Mutex.h> static MyType my_very_important_thing; static CnxMutex* 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(CnxMutex, DEFAULT_ALLOCATOR); *my_thing_mutex = cnx_mutex_new(); cnx_mutex_lock(my_thing_mutex); my_very_important_thing = { // important intialization }; cnx_mutex_unlock(my_thing_mutex); } } void update_my_thing(u64 value) { cnx_mutex_lock(my_thing_mutex); my_very_important_thing.value = value; cnx_mutex_unlock(my_thing_mutex); } u64 get_value_from_my_thing(void) { cnx_mutex_lock(my_thing_mutex); let val = my_very_important_thing.value; cnx_mutex_unlock(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