{
assert(cb_data);
- /* NB: no mutex or waiting here. The callback is ran
+ /* NB: no conditional vars here. The callback is ran
* asynchronously via the main loop. See below,
- * especially `wait_for_all_pending_callbacks`. */
+ * especially `wait_for_all_pending_callbacks`.
+ *
+ * Again, this is an example so you can still run
+ * this sync like the add_user example, and of course
+ * you may still need thread-safety for data access,
+ * such as with the atomic var below. */
g_atomic_int_inc(&callback_pending_reference);
}
/* The wait has to be in a loop because of
- * spurious wakeups, this is a glib issue.
+ * spurious wakeups, this is a general issue
+ * with conditional vars (but keep in mind
+ * it's still better than the alternative as
+ * glib mutexes have undefined behaviour if
+ * locked twice from the same thread).
*
* Of course you can use any synchronisation
* mechanism you want, including none at all. */
bool switch_to_each_user_to_generate_events()
{
+ /* A real-world program will probably just want to switch
+ * to a single specific user; here we are running the gamut
+ * just to generate a bunch of events to show the handlers. */
+
for (int i = firstUser; i <= lastUser; ++i) {
if (!switch_user_test(i)) {
printf("Failed to switch user\n");