C map的线程同步

前端之家收集整理的这篇文章主要介绍了C map的线程同步前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用pthread(c 98标准)创建一个多线程c程序.

我有一个多线程将访问的std :: map.访问将使用find添加删除元素,并使用[]运算符访问元素.

我知道使用[]运算符进行读取,或者甚至使用它修改元素是线程安全的,但其余操作则不是.

第一个问题:我能正确理解吗?

有些线程只能通过[]访问元素,而其他线程则会执行其他一些操作.显然我需要某种形式的线程同步.

我认为这应该工作的方式是:
– 虽然没有对地图进行“写入”操作,但线程应该能够同时从中“读取”.
– 当一个线程想要“写”到映射时,它应该设置一个锁,这样没有线程启动任何“读”或“写”操作,然后它应该等到所有“读”操作都完成,此时它将执行操作并释放锁.
– 释放锁后,所有线程都应该能够自由读取.

主要问题是:我可以使用哪些线程同步方法来实现此行为?

我已经读过有关互斥,条件变量和信号量的内容,据我所知,他们不会做我需要的东西.我熟悉互斥,但不熟悉cond.变量或信号量.

我看到的主要问题是我需要一种锁定线程的方法,直到某些事情发生(写操作结束),然后这些线程依次锁定任何东西.
此外,我需要类似倒置信号量的东西,当计数器大于1时阻塞,然后在它为0时唤醒(即没有进行读取操作).

提前致谢.

附:这是我的第一篇文章.请说明我做错了什么!

解决方法

I understand that reading using the [] operator,or even modifying the elements with it is thread safe,but the rest of the operations are not.

Do I understand this correctly?

那么,你所说的并不完全正确.并发读者可以使用[]访问现有元素,或者如果没有同时的非常量操作(如擦除或插入变异映射<>),则可以安全地使用其他const函数(如find,size()…).并发线程可以修改不同的元素,但是如果一个线程修改了一个元素,则在另一个线程尝试访问或进一步修改该特定元素之前,必须先进行一些同步.

When a thread wants to “write” to the map,it should set a lock so no thread starts any “read” or “write” operation,and then it should wait until all “read” operations have completed,at which point it would perform the operation and release the locks. – After the locks have been released,all threads should be able to read freely.

这不是它的工作方式……为了使作者能够“等到所有”读取“操作完成”,读者需要获得锁定.编写者然后等待释放相同的锁,并自己获取它以限制其他读者或作者,直到他们完成更新并释放它.

what thread synchronisation methods can I use to achieve this behavIoUr?

互斥锁确实是合适的,尽管你经常会从读写器锁中获得更高的性能(它允许并发读者,有些还会优先考虑等待作者而不是其他读者).相关的POSIX线程函数包括pthread_rwlock_rdlock,pthread_rwlock_wrlock,pthread_rwlock_unlock等.

为了对比这两种方法,使用互斥锁的读者和编写者可以得到类似这样的序列化:

THREAD   ACTION
reader1  pthread_mutex_lock(the_mutex) returns having acquired lock,and
         thread starts reading data
reader2  pthread_mutex_lock(the_mutex) "hangs",as blocked by reader1
writer1  pthread_mutex_lock(the_mutex) hangs,as blocked by reader1
reader1  pthread_mutex_unlock(the_mutex) -> releases lock
NOTE: some systems guarantee reader2 will unblock before writer1,some don't
reader2  blocked pthread_mutex_lock(the_mutex) returns having acquired lock,and thread starts reading data
reader1  pthread_mutex_lock(the_mutex) hangs,as blocked by reader2
reader2  pthread_mutex_unlock(the_mutex) -> releases lock    
writer1  blocked pthread_mutex_lock(the_mutex) returns having acquired lock,and thread starts writing and/or reading data
writer1  pthread_mutex_unlock(the_mutex) -> releases lock    
reader1  blocked pthread_mutex_lock(the_mutex) returns having acquired lock,and thread starts reading data
...etc...

使用读写锁定,它可能更像是这样(注意前两个读取器同时运行):

THREAD   ACTION
reader1  pthread_rwlock_rdlock(the_rwlock) returns having acquired lock,and
         thread starts reading data
reader2  pthread_rwlock_rdlock(the_rwlock) returns having acquired lock,and
         thread starts reading data
writer1  pthread_rwlock_wrlock(the_rwlock) hangs,as blocked by reader1/2
reader1  pthread_rwlock_unlock(the_rwlock) -> releases lock
reader1  pthread_rwlock_rwlock(the_rwlock) hangs,as pending writer
reader2  pthread_rwlock_unlock(the_rwlock) -> releases lock    
writer1  blocked pthread_rwlock_wrlock(the_rwlock) returns having acquired lock,and thread starts writing and/or reading data
writer1  pthread_rwlock_unlock(the_rwlock) -> releases lock    
reader1  blocked pthread_rwlock_rwlock(the_rwlock) returns having acquired lock,and thread starts reading data
...etc...

猜你在找的C&C++相关文章