问题描述
有2个线程。线程“ A”是管理器,线程“ B”是工作器。 我在“ B”中创建了GmainContext。然后,我添加了一个超时源并将其附加到“ B”中的GMainContext。
线程A启动。它工作了一段时间,并且想要从“ B”中删除超时源。一段时间后,它需要将相同的源(可能具有相同的源ID)添加回GMainContext中“ B”的相同上下文中。
我认为这是有可能的,但是我不知道要执行什么。
这是我尝试过的。
static GMainLoop* _mainLoop;
static GMainLoop* _taskThreadLoop;
static gboolean _toTaskThread(void* /*data*/)
{
printf("TaskThread\r\n");
return true;
}
static gboolean _toMain(void* /*data*/)
{
printf("Main Thread\r\n");
return true;
}
void* taskThreadMain(void* /*data*/)
{
GMainContext *context = g_main_context_new();
g_main_context_push_thread_default(context);
_taskThreadLoop = g_main_loop_new(context,FALSE);
GSource* toSource = g_timeout_source_new(5000);
g_source_set_callback(toSource,_toTaskThread,nullptr,nullptr);
g_source_attach(toSource,context);
// Run the loop on taskThread
g_main_loop_run(_taskThreadLoop);
// Destroy all the resources used in this thread
g_source_destroy(toSource);
g_main_loop_unref(_taskThreadLoop);
g_main_context_unref(context);
return nullptr;
}
int main()
{
_mainLoop = g_main_loop_new(nullptr,true);
g_timeout_add(2000,_toMain,nullptr);
GThread* taskThread = g_thread_new("thread",taskThreadMain,nullptr);
g_main_loop_run(_mainLoop);
if (_mainLoop)
{
g_main_loop_unref(_mainLoop);
g_main_loop_quit(_taskThreadLoop);
}
g_thread_join(taskThread);
g_thread_unref(taskThread);
return EXIT_SUCCESS;
}
我应该在main()中做什么,以便删除taskThread中的源以及如何将其重新添加。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)