Use mutex or not in a concurrent reading

两盒软妹~` 提交于 2020-01-02 19:23:11

问题


I am programming in C++ in Linux and I am using pthreads library. I am using mutex to protect some shared variables but I am not sure if in this specific case it is necessary the use of mutex.

I have 3 threads. The shared variable is a string (global variable).

Thread1 changes it's value and afterwards, thread2 and thread3 read it's value and store in another string.

In this case, the string's value is only modified by one thread. Is still necessary the use of mutex to protect a shared variable in a concurrent read by two threads?


回答1:


"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.




回答2:


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.




回答3:


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)" 


来源:https://stackoverflow.com/questions/40651592/use-mutex-or-not-in-a-concurrent-reading

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