Use mutex or not in a concurrent reading

醉酒当歌 提交于 2019-12-06 06:05:06

"Thread1 changes it's value and afterwards ..." -- if "afterwards" means that the other threads are created after the change, there's no need for a mutex; thread creation synchronizes memory. If it means anything else then you need some form of synchronization, in part because "afterwards" in different threads is meaningless without synchronization.

What you should use is a shared_mutex (get it from boost if you don't want to use C++14/17) (for C++14 there's a shared_timed_mutex that you could use). Then, you do a shared_lock if you want to read the string, and you do a unique_lock if you want to write on it.

If two shared locks meet, they don't collide and they don't block, but a shared lock and a unique lock collide and one of the locks blocks until the other finishes.

Since you are using pthreads, you can use a pthread_rwlock_t.

For updating the object, it would be locked using pthread_rwlock_wrlock() to get a write lock; all readers would access the object only after obtaining a shared read lock with pthread_rwlock_rdlock(). Since the write lock is exclusive, and the read lock is shared, you'd get the behavior you desire.

An example of the use of pthread_rwlock_t read/write locks can be found at http://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.genprogc/using_readwrite_locks.htm.

A good summary of the available calls for use on a pthread_rwlock_t lock can be found at https://docs.oracle.com/cd/E19455-01/806-5257/6je9h032u/index.html. I've reproduced the table listing the operations:

Operation 

Initialize a read-write lock                   "pthread_rwlock_init(3THR)"

Read lock on read-write lock                   "pthread_rwlock_rdlock(3THR)"

Read lock with a nonblocking read-write lock   "pthread_rwlock_tryrdlock(3THR)"

Write lock on read-write lock                  "pthread_rwlock_wrlock(3THR)"

Write lock with a nonblocking read-write lock  "pthread_rwlock_trywrlock(3THR)"

Unlock a read-write lock                       "pthread_rwlock_unlock(3THR)"

Destroy a read-write lock                      "pthread_rwlock_destroy(3THR)" 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!