I tried the following code snippet.
-----------------------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <glib.h>
static gboolean
g_source_func(gpointer user_data) {
(void)user_data;
printf("%s\n", __func__);
return FALSE;
}
static gpointer
timeout_test_thread(gpointer data) {
int cnt = 0;
GMainLoop *loop = data;
GMainContext *context = g_main_loop_get_context(loop);
while(1) {
printf("%s:%d:%d\n", __func__, __LINE__, cnt);
GSource *timeout = g_timeout_source_new(2000);
g_source_set_callback(timeout, g_source_func, NULL, NULL);
g_source_attach(timeout, context);
g_main_context_iteration(context, TRUE);
g_source_destroy(timeout);
g_source_unref(timeout);
cnt += 1;
}
return NULL;
}
int main(void) {
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
GThread *thread = g_thread_new("timeout_test_thread", timeout_test_thread, loop);
(void)thread;
g_main_loop_run(loop);
g_main_loop_unref(loop);
return 0;
}
-----------------------------------------------------------------
make and run, "g_source_func" printed after 2 seconds once. It is OK.
In NXP environment, I guess the current context is unnecessarily asserted.