结构
GLibStaticRWLock
已弃用:2.32
说明 [src]
struct GStaticRWLock {
/* No available fields */
}
GStaticRWLock
结构表示一个读写锁。读写锁可用于保护部分代码段只读取而其他代码段也写入的数据。在这种情况下,最好让多个读取器可以同时进行读取,当然,每次只能允许一个写入器写入数据。
请看以下示例
GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
GPtrArray *array;
gpointer
my_array_get (guint index)
{
gpointer retval = NULL;
if (!array)
return NULL;
g_static_rw_lock_reader_lock (&rwlock);
if (index < array->len)
retval = g_ptr_array_index (array, index);
g_static_rw_lock_reader_unlock (&rwlock);
return retval;
}
void
my_array_set (guint index, gpointer data)
{
g_static_rw_lock_writer_lock (&rwlock);
if (!array)
array = g_ptr_array_new ();
if (index >= array->len)
g_ptr_array_set_size (array, index + 1);
g_ptr_array_index (array, index) = data;
g_static_rw_lock_writer_unlock (&rwlock);
}
此示例显示了一个可以供多个读取器(my_array_get()
函数)同时访问的数组,而写入器(my_array_set()
函数)一次只能允许一个,并且该数组当前没有读取器访问时才能访问。这是因为数组可能需要重新调整大小。现在,使用这些函数完全支持多线程安全。
大多数情况下,写入器都应优先于读取器。也就是说,对于此实现,一旦写入器想要锁定数据,就不允许任何其他读取器锁定数据,当然,已经锁定数据的读取器可以完成其操作。一旦最后一个读取器解锁数据,写入器将对其加锁。
尽管 GStaticRWLock
不是不透明的,但它只应与以下函数一起使用。
即使没有调用 g_thread_init()
,所有 g_static_rw_lock_
函数都可以使用。然后它们不会执行任何操作,除了 g_static_rw_lock_
_trylock,它的作用是什么也不做,除了返回 TRUE
。
与互斥锁相比,读写锁的开销更大。例如,g_static_rw_lock_reader_lock()
和 g_static_rw_lock_reader_unlock()
都必须锁定和解锁一个 GStaticMutex
,因此,锁定和解锁 GStaticRWLock
所花费的时间至少为锁定和解锁 GStaticMutex
所花费时间的两倍。因此,只有由多个读取器访问并保持锁较长时间的数据结构才合理使用 GStaticRWLock
。上面的示例可能更适合使用 GStaticMutex
。
已弃用:2.32。
改用 GRWLock
。
实例方法
g_static_rw_lock_reader_lock
Lock the lock
for reading. At the same time, a GStaticRWLock
can have any number of concurrent read locks. If the lock
is already locked for writing by another thread or if another thread will wait to lock lock
for writing, this function will block until lock
is unlocked by the other writing thread and no other writing threads are willing to lock lock
. This lock must be unlocked by g_static_rw_lock_reader_unlock().
已弃用:2.32
g_static_rw_lock_reader_trylock
Try to lock lock
for reading. If lock
is already locked for writing by another thread or if another thread will wait to lock lock
for writing, the function will return FALSE
immediately. Otherwise, lock lock
for reading and return TRUE
. This lock must be unlocked by g_static_rw_lock_reader_unlock().
已弃用:2.32
g_static_rw_lock_reader_unlock
Unlock lock
. If a thread waits to lock lock
for writing and all read locks have been unlocked, the waiting thread is waked up and can lock lock
for writing.
已弃用:2.32
g_static_rw_lock_writer_lock
Lock lock
for writing. If lock
is already locked for writing or reading by other threads, the function will block until lock
is fully unlocked, and then lock lock
for writing. While this function waits to lock lock
, no other thread can lock lock
for reading. When lock
is locked for writing, no other thread can lock lock
(even for reading or writing). This lock must be unlocked by g_static_rw_lock_writer_unlock().
已弃用:2.32
g_static_rw_lock_writer_trylock
Try to lock lock
for writing. If lock
is already locked (for reading or writing) by another thread, the function will return FALSE
immediately. Otherwise, lock lock
for writing and return TRUE
. This lock must be unlocked by g_static_rw_lock_writer_unlock().
已弃用:2.32
g_static_rw_lock_writer_unlock
Unlock lock
. If a thread waits to lock lock
for writing and all read locks have been unlocked, the waiting thread is waked up and can lock lock
for writing. If no thread is waiting to lock lock
for writing and multiple threads are waiting to lock lock
for reading, the waiting threads are waked up and can lock lock
for reading.
已弃用:2.32