Optimization of queue under multi-thread

Source: Internet
Author: User

0. Preface

The previous time in looking at the lock-free queue related things to find an article on the Lock queue algorithm optimization, so look at its principle and use C to implement the queue. The queue is linkedblockingqueue in Java and implements this functionality.

Related article: Simple, Fast, and practical non-blocking and Blocking Concurrent Queue algorithms

Algorithm optimization of multi-thread queue

1. Fundamentals

For multi-threading, multiple producers and multi-consumer environments, can be optimized by separating the lock at the head of the team and the end of the team. Initially, using an empty node, when inserting an element into an empty queue or ejecting the last element node, you do not need to acquire both sides of the lock (because the tail and head pointers need to be modified at the same time, but an empty node can prevent this from happening).

2. C Simple Implementation

#include <pthread.h>#include<stdlib.h>#include<assert.h>typedefstructqnode{void*PValue; structQnode *Pnext;} Qnode;typedefstructqueue{Qnode*Phead; Qnode*Ptail;    pthread_mutex_t headlock; pthread_mutex_t Taillock;} Tqueue; Tqueue*Inittqueue () {Tqueue* Pqueue =malloc(sizeof(*pqueue));    ASSERT (Pqueue); Qnode*pemptynode =malloc(sizeof(*Pemptynode));    ASSERT (Pemptynode); Pemptynode->pnext =NULL; Pqueue->phead = Pqueue->ptail =Pemptynode; Pthread_mutex_init (&pQueue->headlock, NULL); Pthread_mutex_init (&pQueue->Taillock, NULL); returnPqueue;}voidEnQueue (Tqueue *pqueue,void*pValue)    {assert (Pqueue); Qnode*pnewnode =malloc(sizeof(*Pnewnode)); Pnewnode->pvalue =PValue; Pnewnode->pnext =NULL; Pthread_mutex_lock (&pQueue->Taillock); Pqueue->ptail->pnext =Pnewnode; Pqueue->ptail =Pnewnode; Pthread_mutex_unlock (&pQueue->taillock);}void* DeQueue (Tqueue *pqueue) {    void*pvalue =NULL; Pthread_mutex_lock (&pQueue->headlock); Qnode*ppopnode = pqueue->Phead; if(NULL = = ppopnode->Pnext) {Pthread_mutex_unlock (&pQueue->headlock); returnNULL; } pValue= ppopnode->pnext->PValue; Pqueue->phead = ppopnode->Pnext; Pthread_mutex_unlock (&pQueue->headlock);  Free(Ppopnode); returnPValue;}void* Producer (void*parg) {Tqueue* Pqueue = (tqueue*) parg; Const intNmsgnums =Ten;  for(inti =1; I <= nmsgnums; ++i) {EnQueue (Pqueue, (void*) (i); } EnQueue (Pqueue,0);}void* Consumer (void*parg) {Tqueue* Pqueue = (tqueue*) parg;  while(1)    {        intNmsg = (int) DeQueue (pqueue); if(Nmsg = =0)        {             Break; } printf ("%u msg[%d]\n", Pthread_self (), nmsg); }}#defineMax_pthreads 3intMainintargcChar Const*argv[]) {Tqueue* Pqueue =Inittqueue ();    ASSERT (Pqueue);    pthread_t Producerid[max_pthreads];    pthread_t Consumerid[max_pthreads];  for(inti =0; i < max_pthreads; ++i) {pthread_create (&producerid[i], NULL, Producer, (void*) pqueue); Pthread_create (&consumerid[i], NULL, Consumer, (void*) pqueue); }     for(inti =0; i < max_pthreads; ++i) {pthread_join (producerid[i], NULL);    Pthread_join (Consumerid[i], NULL); }    return 0;}

Optimization of queue under multi-thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.