Locking mechanism - read "Pattern-Oriented Software Architecture: Volume 2" felt

Recent reading always bits and pieces, so develop a habit of issuing a blog after reading it, record it.

1, delimitation lock

Very big name on, in fact, locked with the class constructor, destructor releases the lock.
It is an advantage of object-oriented, or prevent some statements out of the current range abnormality, the lock is not released.

2, the policy of locking

Components sometimes require different synchronization features: mutexes, read-write locks, lights (lights is what the hell then check when it is used?) And air lock.
As we all know, code reuse the main mode of inheritance, portfolio template.
In order to use different locks, here we talked about in two ways: and parameterized types (actually a template) (actually a combination) polymorphism.
I like writing template here so briefly write about.

typedef fileCache<nullMutex> contentCache
typedef fileCache<threadMutex> contentCache
typedef fileCache<rwMutex> contextCache

When this component is used somewhere in a certain characteristic, you can typedef it, very beautiful and clear Kazakhstan.

3, thread-safe interfaces

Multiple methods in a class, they may call each other, resulting in a deadlock since.
Method 1:
  instead of recursive lock, but the book says this method to achieve the efficiency is low. (Carefully study confirm)
Method 2:
 all locks on the interface method, after acquiring the lock, perform the actual operation by implementing methods (private or protected), after returning release the lock.
 Advantages: implementation of clear thinking, because of its trusted interface method, so do not lock.
 Disadvantages: the larger locking range of the code, and transmits not very efficient.

4, double lock check Optimization

The book also speaks very general and one-sided, according to my own knowledge to write it.
Single embodiment mode as an example, is divided into lazy and hungry man.
Starving:
static variables function in vitro, direct new new (), corresponding to a global variable.
Thread safety. The disadvantage is that if a single case has been less than, a bit wasted.
In addition, the order did not specify a different configuration file in the global variable (c ++ in all the variables is how to construct, I really do not know, unlike c, compiler theory glance), sometimes does not work.
Lazy:
Singleton used only to initialize.

static xxx* instance() {
	if (instance_ == null) {
		std::lock_guard(mutex_);
		if (instance_ == null)
			instance = new xxx();
	}
	return instance_;
}

The reason is checked twice: when multiple threads simultaneously initialization, if passed the first examination, would have queued lock, when the first lock to get the thread is instantiated, other threads will be skipped. Without the second inspection, the inevitable GG.
It is also conceivable to add lock out the first inspection, the efficiency is very low.
Question:
  Some compilers platform may be a problem. memory write operation is not atomic after new, if time coincidence, problems may arise; coherence multiprocessor cache (what the hell, can not read).

Guess you like

Origin blog.csdn.net/weixin_42238721/article/details/91817757