Locked write, unlocked read

Algorithmic question

How to allow only the following types of threaded operations on an object?

  • multiple simultaneous reads, no writes
  • single write, no reads

Example: wrapper for STL container allowing efficient search from multiple threads. For simplicity, let assume no iterator can be accessed from outside of the wrapper in question.

Let's assume we have semaphores and mutexes at out disposal.

I know that boost libraries has this concept implemented. I'd like to understand how is this usually done.

Answers


Use boost::shared_mutex to handle frequent read, infrequent write access patterns.

As you've noted, STL containers are 'leaky' in that you can retrieve an iterator which has to be treated as either an implicit ongoing operation for write (if non-const) or read (if const), until the iterator goes out of scope. Writes to the container by other threads while you hold such an iterator can invalidate it. Your wrapper would have to be carefully designed to handle this case and keep the wrapper class efficient.


You want a "multiple-reader / single-writer" mutex : Boost.Thread provides one.


Need Your Help

Custom Listview inside an AlertDialog hides dialog's message?

android alertdialog android-alertdialog

I am working on an application in which I have a list populated in an AlertDialog. On some event the dialog appears, showing its Title, Message and the Custom listview. Problem is, when I add more ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.