简体   繁体   中英

GTK 3 Threads management

I know that before we needed to use gdk_threads_enter and gdk_threads_leave but now this functions are deprecated and I'm confused about threads.

Should i just add threads with g_idle_add and that's it?

For workers that are unrelated to UI stuff, use the GThread API, for anything UI related you have to use g_idle_add / g_timeout_add or attach a custom GSource to the desired event loop. In some cases you'd want to combine those - do some work in a GThread and notify the UI from there - as soon as the worker has finished - via g_idle_add .

If you are developing a library you could also implement it in a very clean and cancelable manner via the GAsyncInitableIface (though that requires a bit more reading before starting to actually implement it).


In response to @ptomato's comment:

You should use gdk_threads_add_idle() and gdk_threads_add_timeout() instead of g_idle_add() and g_timeout_add() since libraries not under your control might be using the deprecated GDK locking mechanism. If you are sure that none of the code in your application and libraries use the deprecated gdk_threads_enter() or gdk_threads_leave() methods, then you can safely use g_idle_add() and g_timeout_add().

使用g_thread_new (name, func, data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM