为什么使用 `g_application_run` 会产生约 25 秒的延迟?
我正在尝试学习 GTK3,但文档充其量只是有问题。最令人沮丧的问题来自于使用g_application_run
。程序运行时似乎会产生约 25 秒的延迟。
这是示例:
#include <gtk/gtk.h>
static void activate(GtkApplication *app, gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_widget_show_all(window);
exit(0);
}
int main(int argc, char **argv)
{
GtkApplication *app;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return 0;
}
非常简单,它创建一个窗口,显示它并退出。主要测试加载时间。当使用 time
运行时,我得到以下结果:
$ time ./capplication_new
real 0m25.177s
user 0m0.151s
sys 0m0.014s
程序运行超过 25 秒。这实在令人难以接受。最令人沮丧的是,它是您应该开始使用的最新代码。但是,如果我运行这个创建窗口、显示窗口并退出的最小示例:
#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show(window);
exit(0);
gtk_main();
}
当使用 time
运行时,结果是:
$ time ./binit
real 0m0.139s
user 0m0.108s
sys 0m0.023s
有没有办法阻止它滞后?为什么会滞后?如果它滞后,为什么这是新代码?
**编辑:
$ uname -a
Linux testing 5.10.0-11-amd64 #1 SMP Debian 5.10.92-1 (2022-01-18) x86_64 GNU/Linux
I'm trying to learn GTK3, but the documentation is problematic at best. The most frustrating problem comes from using g_application_run
. It seems to create a lag of ~25 seconds when the program is run.
Here is the example:
#include <gtk/gtk.h>
static void activate(GtkApplication *app, gpointer user_data)
{
GtkWidget *window;
window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
gtk_widget_show_all(window);
exit(0);
}
int main(int argc, char **argv)
{
GtkApplication *app;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return 0;
}
Very minimal, it creates a window, shows it and exits. Basically testing the time to load. When run with time
I get the following results:
$ time ./capplication_new
real 0m25.177s
user 0m0.151s
sys 0m0.014s
Over 25 seconds for the program to run. This is really unacceptable. What is most frustrating about this is that it's the newest code you're supposed to start using. But if I run this minimal example of creating a window, showing it and exiting:
#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show(window);
exit(0);
gtk_main();
}
When run with time
, the results are:
$ time ./binit
real 0m0.139s
user 0m0.108s
sys 0m0.023s
Is there a way to stop it from lagging? Why is it lagging? Why is this the new code if it lags?
**EDIT:
$ uname -a
Linux testing 5.10.0-11-amd64 #1 SMP Debian 5.10.92-1 (2022-01-18) x86_64 GNU/Linux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要找出为什么它在您的计算机上滞后而不是在其他人的计算机上滞后,您将需要进行更多调试。我建议从
ltrace
(您可能需要安装)开始。您可以运行:这应该会显示应用程序内进行的所有库调用,并帮助突出显示导致延迟的调用。
从您的计时中我们可以看到,您的应用几乎在 25 秒的时间里除了等待其他事情之外什么也没做。希望 ltrace 能够帮助确定那是什么。当我们知道这一点时,我们就可以想出如何解决它。
To find out why it is lagging on your machine and not on other peoples' machines you will need to do some more debugging. I suggest starting with
ltrace
(which you may need to install). You can run:That should show you all the library calls being made inside your application and help highlight the ones causing the delay.
From your timings we can see that your app is spending almost all of the 25 seconds doing nothing but waiting on something else. Hopefully the
ltrace
will help determine what that is. When we know that, we can work out how to fix it.我不知道为什么会发生这种情况,但我找到了解决方案。
您需要将以下内容添加到
.xinitrc
或.xsession
文件中:I didn't find out why it was happening, but I was able to find a solution.
You need to add the following to your
.xinitrc
or.xsession
file: