Parsing C + + non-lock queue implementation code _c language

Source: Internet
Author: User
This paper presents a C + + lock-free queue implementation code, which is mainly used for one thread to read data and another thread to write data
Copy Code code as follows:

#ifndef Lock_free_queue_h_
#define Lock_free_queue_h_

No lock queue, suitable for one thread read, one thread write
#include <list>
Template <typename t>
Class Lockfreequeue
{
Public
Lockfreequeue ()
{
List.push_back (T ());//Split node
Ihead = List.begin ();
Itail = List.end ();
};

void Produce (const t& T)//Save message
{
List.push_back (t);
Itail = List.end ();
List.erase (List.begin (), ihead);
};

BOOL Consume (t& T)//Fetch message
{
TypeName Tlist::iterator inext = Ihead;
++inext;
if (Inext!= itail)
{
Ihead = Inext;
t = *ihead;
return true;
}
return false;
};

BOOL Peek (t& T)//view message not deleted
{
TypeName Tlist::iterator inext = Ihead;
++inext;
if (Inext!= itail)
{
t = *inext;
return true;
}
return false;
}

BOOL IsEmpty ()
{
TypeName Tlist::iterator inext = Ihead;
++inext;
if (Inext!= itail)
{
return false;
}
Else
{
return true;
}
}

int Getmaxsize ()
{
return List.max_size ();
};

Private
typedef std::list<t> TLIST;
Tlist list;
TypeName Tlist::iterator Ihead, Itail;
};
#endif

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.