⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 async_queues.sgml

📁 GLib是GTK+和GNOME工程的基础底层核心程序库
💻 SGML
字号:
<!-- ##### SECTION Title ##### -->Asynchronous Queues<!-- ##### SECTION Short_Description ##### -->asynchronous communication between threads.<!-- ##### SECTION Long_Description ##### --><para>Often you need to communicate between different threads. In generalit's safer not to do this by shared memory, but by explicit messagepassing. These messages only make sense asynchronously formulti-threaded applications though, as a synchronous operation could aswell be done in the same thread.</para><para>Asynchronous queues are an exception from most other GLib datastructures, as they can be used simultaneously from multiple threadswithout explicit locking and they bring their own builtin referencecounting. This is because the nature of an asynchronous queue is thatit will always be used by at least 2 concurrent threads.</para><para>For using an asynchronous queue you first have to create one withg_async_queue_new(). A newly-created queue will get the referencecount 1. Whenever another thread is creating a new reference of (thatis, pointer to) the queue, it has to increase the reference count(using g_async_queue_ref()). Also, before removing this reference, thereference count has to be decreased (usingg_async_queue_unref()). After that the queue might no longer exist soyou must not access it after that point.</para><para>A thread, which wants to send a message to that queue simply callsg_async_queue_push() to push the message to the queue.</para><para>A thread, which is expecting messages from an asynchronous queuesimply calls g_async_queue_pop() for that queue. If no message isavailable in the queue at that point, the thread is now put to sleepuntil a message arrives. The message will be removed from the queueand returned. The functions g_async_queue_try_pop() andg_async_queue_timed_pop() can be used to only check for the presenceof messages or to only wait a certain time for messages respectively.</para><para>For almost every function there exist two variants, one that locks thequeue and one that doesn't. That way you can hold the queue lock(acquire it with g_async_queue_lock() and release it withg_async_queue_unlock()) over multiple queue accessinginstructions. This can be necessary to ensure the integrity of thequeue, but should only be used when really necessary, as it can makeyour life harder if used unwisely. Normally you should only use thelocking function variants (those without the suffix _unlocked)</para><!-- ##### SECTION See_Also ##### --><para></para><!-- ##### STRUCT GAsyncQueue ##### --><para>The #GAsyncQueue struct is an opaque data structure, which representsan asynchronous queue. It should only be accessed through the<function>g_async_queue_*</function> functions.</para><!-- ##### FUNCTION g_async_queue_new ##### -->@Returns: <!-- ##### FUNCTION g_async_queue_ref ##### -->@queue: <!-- ##### FUNCTION g_async_queue_unref ##### -->@queue: <!-- ##### FUNCTION g_async_queue_push ##### -->@queue: @data: <!-- ##### FUNCTION g_async_queue_pop ##### -->@queue: @Returns: <!-- ##### FUNCTION g_async_queue_try_pop ##### -->@queue: @Returns: <!-- ##### FUNCTION g_async_queue_timed_pop ##### -->@queue: @end_time: @Returns: <!-- ##### FUNCTION g_async_queue_length ##### -->@queue: @Returns: <!-- ##### FUNCTION g_async_queue_lock ##### -->@queue: <!-- ##### FUNCTION g_async_queue_unlock ##### -->@queue: <!-- ##### FUNCTION g_async_queue_ref_unlocked ##### -->@queue: <!-- ##### FUNCTION g_async_queue_unref_and_unlock ##### -->@queue: <!-- ##### FUNCTION g_async_queue_push_unlocked ##### -->@queue: @data: <!-- ##### FUNCTION g_async_queue_pop_unlocked ##### -->@queue: @Returns: <!-- ##### FUNCTION g_async_queue_try_pop_unlocked ##### -->@queue: @Returns: <!-- ##### FUNCTION g_async_queue_timed_pop_unlocked ##### -->@queue: @end_time: @Returns: <!-- ##### FUNCTION g_async_queue_length_unlocked ##### -->@queue: @Returns: 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -