如何在不锁定应用程序的情况下使用大型数据集填充 Gtk::TreeModelColumn
我需要将一个大的(也许不是那么多 - 几千个条目)数据集填充到 Gtk::TreeModelColumn 中。如何在不锁定应用程序的情况下做到这一点?将处理放入单独的线程中安全吗?那么我必须用锁保护应用程序的哪些部分?它只是 Gtk::TreemodelColumn 类,还是它所在的 Gtk::TreeView 小部件,甚至可能是周围的框架或窗口?
I need to fill in a large (maybe not so much - several thousands of entries) dataset to a Gtk::TreeModelColumn. How do I do that without locking up the application. Is it safe to put the processing into separate thread? What parts of the application do I have to protect with a lock then? Is it only the Gtk::TreemodelColumn class, or Gtk::TreeView widget it is placed in, or maybe even surrounding frame or window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以采取两种一般方法。 (免责声明:我试图提供示例代码,但我很少使用 gtkmm - 我对 C 语言中的 GTK 更熟悉。但是原理保持不变。)
一种是使用 空闲函数 - 当 GUI 中没有发生任何事情时运行。为了获得最佳结果,请在空闲函数中进行少量计算,例如将一项添加到树视图中。如果您从空闲函数返回
true
,那么每当有更多可用处理时间时就会再次调用它。如果返回false
,则不会再次调用。空闲函数的好处是您不必锁定任何内容。因此,您可以像这样定义空闲函数:然后像这样启动进程:
另一种方法是使用线程。在 C API 中,这将涉及到 gdk_threads_enter() 等,但我认为在 gtkmm 中执行此操作的正确方法是使用 Glib::Dispatcher。我以前没有使用过它,但是这里是一个例子。但是,您仍然可以将 C API 与 gtkmm 结合使用,如 这里。
There are two general approaches you could take. (Disclaimer: I've tried to provide example code, but I rarely use gtkmm - I'm much more familiar with GTK in C. The principles remain the same, however.)
One is to use an idle function - that runs whenever nothing's happening in your GUI. For best results, do a small amount of calculation in the idle function, like adding one item to your treeview. If you return
true
from the idle function, then it is called again whenever there is more processing time available. If you returnfalse
, then it is not called again. The good part about idle functions is that you don't have to lock anything. So you can define your idle function like this:Then start the process like this:
The other approach is to use threads. In the C API, this would involve
gdk_threads_enter()
and friends, but I gather that the proper way to do that in gtkmm, is to useGlib::Dispatcher
. I haven't used it before, but here is an example of it. However, you can also still use the C API with gtkmm, as pointed out here.