About the concept and implementation of the lock-free queue, you can refer to the blog "The implementation of the lock-free queue," mainly related to the knowledge points include CAs atomic operations, chain list implementation without lock queue, array implementation of the lock-free queue and ABA problem.
The following reference to the "multi-threaded thing (no lock queue)" code, that two threads (one to add a read data) between the lock-free queue, you can not use the thread mutex method to achieve the parallel effect. The code is as follows:
#defineMax_number 1000L#defineSTATUS int#defineOK 0#defineFALSE-1typedefstruct_queue_data{intData[max_number]; intHead; inttail;} Queue_data; STATUS Push_data (Queue_data* Pqueue,intdata) { if(NULL = =pqueue)returnERROR; if(Pqueue->head = = ((pqueue->tail) +1)%max_number)returnERROR; Pqueue->data[pqueue->tail] =data; Pqueue->tail = (Pqueue->tail +1)%Max_number; returnOK;} STATUS Pop_data (Queue_data* Pqueue,int*pData) { if(NULL = = Pqueue | | NULL = =pData)returnERROR; if(Pqueue->head = = pqueue->tail)returnERROR; *pdata = pqueue->data[pqueue->Head]; Pqueue->head = (Pqueue->head +1)%Max_number; returnOK;}View Code
Summary:
- The lock-free queue is only suitable for two threads in parallel, one press-in data, one popup data
- Lock-free queue is a parallel without locks, and there is no danger of deadlock
- The head and tail in the lock-free queue can only perform the self-increment operation until the end of the calculation
The non-locking queue for multithreaded programming