九、GLib——GMainLoop
1 GMainLoop相关结构体
/* filename: gmain.c */
struct _GMainLoop
{
GMainContext *context;
gboolean is_running; /* (atomic) */ /* 循环函数是否正在运行 */
gint ref_count; /* (atomic) */
};
引用计数:
GMainLoop *loop = g_main_loop_new(NULL, TRUE); /* 引用计数加一,loop->ref_count = 1 */
/* g_main_loop_run循环函数进入时候,引用计数加一, loop->ref_count = 2
* g_main_loop_run循环函数最后退出之前,引用计数减一, loop->ref_count = 1
*/
g_main_loop_run (loop); 、
/* 此时, loop->ref_count = 1 */
g_main_loop_unref (loop); /* 这里解引用loop的时候,会把context解引用一次,context里面会把source解引用一次 */
2 GMainLoop相关函数
/***
* @param context(nullable): 设置为NULL,使用全局默认上下文,也可以添加自己创建的context
* @param is_running: 这个设不设置都可以,因为 g_main_loop_run() 会设置 loop->is_running = TRUE
*/
GMainLoop *
g_main_loop_new (GMainContext *context,
gboolean is_running);
/**
* @brief: 会循环一直判断 loop->is_running 是否成立,如果成立执行 g_main_context_iterate ()
*/
void
g_main_loop_run (GMainLoop *loop);
/**
* @brief: 退出循环函数,也就是 loop->is_running = FALSE
*/
void
g_main_loop_quit (GMainLoop *loop);
/**
* @brief: 获取 loop->is_running 的值
*/
gboolean
g_main_loop_is_running (GMainLoop *loop);