multithreading - boost shared_mutex destructor -


I have a multithreaded app that often needs to read some data, and sometimes that data is updated Using the upgrade_lock, I have problems using unique_lock and problems. I have an example of my problems:

  zero unlock () {test.stream = 0; Test.mtx.unlock (); } Zero lock_mtx (int i) {boost :: unique_lock & lt; Boost :: shared_mutex & gt; (Test.mtx) lock; Test.stream = i; Promote: this_thread :: sleep_for (boost :: chrono :: milliseconds (10000)); Unlock (); Promote: this_thread :: sleep_for (boost :: chrono :: milliseconds (1000)); }   

When I destroy lock , then the Mute X is already unlocked with this thread, and sometimes it's another thread Is locked from, but the district makes it free again. After the destruction of the lock (in the first thread) the third thread takes Mute X and I have two authors in a moment:

  Zero lock MMTX (int i) (boost :: upgrade_lock & lt; Boost :: shared_mutex> lock (test.mtx); read_from_locked (); to promote: this_thread :: sleep_for (boost :: chrono :: milliseconds (5000)); promotion: upgrade_to_unique_lock & lt; promotion Giving :: share_mutex> uniqueLock (lock); write_to_locked (); to promote: this_thread :: sleep_for (boost :: chrono :: milliseconds (10000));}   

The other problem is that when some thread

Div class = "post-text" itemprop = "text">

  zero lock_max (int i) {{boost :: unique_lock & quot; promotion :: shared_mutex> (Test.mtx) lock; Test.stream = i; Promote :: this_thread :: sleep_for (boost :: chrono :: milliseconds (10000)); } Boost :: this_thread :: sleep_for (boost :: chrono :: milliseconds (1000)); } The purpose of   

unique_lock and lock_guard is to automatically unlock the Mute X when it gets out of range This pattern is known as RAII.

Rule of thumb: Never call objects on lock () / unlock () manually. This is an opponent because

  • It is very difficult to get the right (think about exception protection)
  • This is usually a code odor (call different method If you also need it, consider creating a member of the class with the ROI lock guard ( lock_guard or unique_lock ), or Unique_lock is the option of obviously adoption in the lock near the collar, or it By Ard is automatically released.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -