Chapter 7 is designed to lock-free concurrent data structures

7.1 Definitions and results

Mutex element, algorithms and data structures, and condition variables for future synchronization data is called blocking of algorithms and data structures.

Data structures and algorithms is no blocking library functions are called non-blocking, but not all of these data structures are no lock.

7.1.1 Non-blocking data structure type

You can use std :: atomic_flag as a basic spin lock mutex yuan

class spinlock_mutex{
	
	std::atomic_flag flag;
	
	public:
		spinlock_mutex(): flag(ATOMIC_FLAG_INIT){}
		void lock(){
			while(flag.test_and_set(std::memory_order_acquire));
		}
		
		void unlock(){
			flag.clear(std::memory_order_release);
		}
};

Using cyclic form is not blocked.

7.1.2 no lock data structures

For eligible to be a lock-free data structures, it must be able to make more than one concurrent thread can access this data structure. Used in the data structure comparison / switching operation algorithm which often contains a loop. Comparative / swap operation is possible because while another thread is modifying the data, in this case, the code will need to try to re-compare / redo part of the operation before the exchange, if the comparison / exchange operations eventually interrupted in other threads the success of the case, so that the code is no lock. If not, at least using a spin, rather than a non-blocking lock-free.

7.1.3 no waiting data structure

A non-lock without waiting for the data structure of a data structure, and has additional features, each thread can access the data structure to complete its operations in a finite number of steps, but do not control the behavior of other threads. Because of conflicts with other threads that may be involved in an unlimited number of retries algorithm is not no waiting.

7.1.4 advantages and disadvantages of lock-free data structures

    1. The reason for using lock-free data structures is to achieve the greatest degree of concurrency.
    2. Robustness, a lock-free data structures in the case of using a lock, and when to terminate, the data structure to be permanently destroyed without destroying the lock data structure is only data.
    3. Because it is not a lock, a deadlock does not occur
    4. But will also appear live lock, that when two threads to modify the data for each thread since the other thread did change will require the operation of this thread is re-executed, these two threads are endless repetition.

Examples of lock-free data structures 7.2

7.2.1 writing thread-safe stack without lock

7.2.2 Stop leak annoying: managing memory in lock-free data structures

// code has time to fix

Guess you like

Origin www.cnblogs.com/hebust-fengyu/p/12087832.html