如何监视Gtk3事件循环延迟 [英] How to monitor Gtk3 Event Loop latency

查看:78
本文介绍了如何监视Gtk3事件循环延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想监视Gtk3事件循环的延迟,即Gtk主事件循环的每次迭代所花费的时间.基本上,这个想法是在主事件循环的每个时刻运行一个自定义函数.

I would like to monitor Gtk3 event loop latency, i.e time spent for each iteration of Gtk main event loop. Basically, the idea is to run a custom function at each tick of the main event loop.

我尝试了 g_idle_add ,但是尚不清楚文档是否在每个循环中调用

有什么想法吗?

推荐答案

可能最好编写一个自定义的 GSource .

Probably writing a custom GSource is your best choice.

GSource *
g_source_new (GSourceFuncs *source_funcs,
             guint struct_size);

指定大小以允许创建从GSource派生的包含其他数据的结构

The size is specified to allow creating structures derived from GSource that contain additional data

您还应该赋予它最高优先级.我不确定它是否会在每次迭代中分派,但是会在每次迭代中进行 prepared .为了使您的 source 栩栩如生,您可以使用 g_main_loop_get_context 获取上下文,然后调用 g_source_attach .

You should also give it the highest priority. I'm not sure it will be dispatched at every single iteration, but it will be prepared on every iteration. To bring your source to life you obtain context with g_main_loop_get_context and call g_source_attach.

总的来说,看起来像这样:

All in all it looks like this:

// struct MySource
// {
//  struct GSource glib;
//  int            my_data;
// };


gboolean my_prepare (GSource *source,
                     gint    *timeout_)
{
  g_message ("%li", g_get_monotonic_time());
  *timeout_ = 0;
  (MySource*)source->my_data = 1;
  return TRUE;
}


GSourceFuncs funcs = {.prepare = my_prepare};

GSource *src = g_source_new (&funcs, sizeof (MySource));
g_source_set_priority (src, G_PRIORITY_HIGH);
g_source_attach (src, g_main_loop_get_context());

这不包括任何清理工作.

This doesn't include any cleanup.

这篇关于如何监视Gtk3事件循环延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆