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

📄 atomic_operations.sgml

📁 This GLib version 2.16.1. GLib is the low-level core library that forms the basis for projects such
💻 SGML
字号:
<!-- ##### SECTION Title ##### -->Atomic Operations<!-- ##### SECTION Short_Description ##### -->basic atomic integer and pointer operations<!-- ##### SECTION Long_Description ##### --><para>The following functions can be used to atomically access integers andpointers. They are implemented as inline assembler function on mostplatforms and use slower fall-backs otherwise. Using them can sometimessave you from using a performance-expensive #GMutex to protect theinteger or pointer.  </para><para>The most important usage is reference counting. Usingg_atomic_int_inc() and g_atomic_int_dec_and_test() makes referencecounting a very fast operation.</para><note><para>You must not directly read integers or pointers concurrently accessedby multiple threads, but use the atomic accessor functions instead.That is, always use g_atomic_int_get() and g_atomic_pointer_get() forread outs.They provide the neccessary synchonization mechanisms like memorybarriers to access memory locations concurrently.</para> </note><note><para>If you are using those functions for anything apart from simplereference counting, you should really be aware of the implications ofdoing that. There are literally thousands of ways to shoot yourself inthe foot. So if in doubt, use a #GMutex. If you don't know, whatmemory barriers are, do not use anything but g_atomic_int_inc() andg_atomic_int_dec_and_test().</para> </note><note><para>It is not safe to set an integer or pointer just by assigning to it,when it is concurrently accessed by other threads with the followingfunctions. Use g_atomic_int_compare_and_exchange() org_atomic_pointer_compare_and_exchange() respectively.</para> </note><!-- ##### SECTION See_Also ##### --><para><variablelist> <varlistentry><term>#GMutex</term><listitem><para>GLib mutual exclusions.</para></listitem></varlistentry> </variablelist></para><!-- ##### SECTION Stability_Level ##### --><!-- ##### FUNCTION g_atomic_int_get ##### --><para>Reads the value of the integer pointed to by @atomic. Also acts asa memory barrier.</para>@atomic: a pointer to an integer@Returns: the value of *@atomic@Since: 2.4<!-- ##### FUNCTION g_atomic_int_set ##### --><para>Sets the value of the integer pointed to by @atomic. Also acts as a memory barrier.</para>@atomic: a pointer to an integer@newval: the new value@Since: 2.10<!-- ##### FUNCTION g_atomic_int_add ##### --><para>Atomically adds @val to the integer pointed to by @atomic.Also acts as a memory barrier.</para>@atomic: a pointer to an integer.@val: the value to add to *@atomic.@Since: 2.4<!-- ##### FUNCTION g_atomic_int_exchange_and_add ##### --><para>Atomically adds @val to the integer pointed to by @atomic. It returnsthe value of *@atomic just before the addition took place.Also acts as a memory barrier.</para>@atomic: a pointer to an integer.@val: the value to add to *@atomic.@Returns: the value of *@atomic before the addition.@Since: 2.4<!-- ##### FUNCTION g_atomic_int_compare_and_exchange ##### --><para>Compares @oldval with the integer pointed to by @atomic andif they are equal, atomically exchanges *@atomic with @newval.Also acts as a memory barrier.</para>@atomic: a pointer to an integer.@oldval: the assumed old value of *@atomic.@newval: the new value of *@atomic.@Returns: %TRUE, if *@atomic was equal @oldval. %FALSE otherwise.@Since: 2.4<!-- ##### FUNCTION g_atomic_pointer_get ##### --><para>Reads the value of the pointer pointed to by @atomic. Also acts asa memory barrier.</para>@atomic: a pointer to a #gpointer.@Returns: the value to add to *@atomic.@Since: 2.4<!-- ##### FUNCTION g_atomic_pointer_set ##### --><para>Sets the value of the pointer pointed to by @atomic. Also acts as a memory barrier.</para>@atomic: a pointer to a #gpointer@newval: the new value@Since: 2.10<!-- ##### FUNCTION g_atomic_pointer_compare_and_exchange ##### --><para>Compares @oldval with the pointer pointed to by @atomic andif they are equal, atomically exchanges *@atomic with @newval. Also acts as a memory barrier.</para>@atomic: a pointer to a #gpointer.@oldval: the assumed old value of *@atomic.@newval: the new value of *@atomic.@Returns: %TRUE, if *@atomic was equal @oldval. %FALSE otherwise.@Since: 2.4<!-- ##### FUNCTION g_atomic_int_inc ##### --><para>Atomically increments the integer pointed to by @atomic by 1.</para>@atomic: a pointer to an integer.@Since: 2.4<!-- ##### FUNCTION g_atomic_int_dec_and_test ##### --><para>Atomically decrements the integer pointed to by @atomic by 1.</para>@atomic: a pointer to an integer.@Returns: %TRUE, if the integer pointed to by @atomic is 0 afterdecrementing it.@Since: 2.4

⌨️ 快捷键说明

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