Boost无锁队列
在开发接收转发agent时,采用了多线程的生产者-消费者模式,用了加互斥锁的方式来实现线程同步。互斥锁会阻塞线程,所以压测时,效率并不高。所以想起用无锁队列来实现,性能确实提升了。
首先介绍下lock-free和wait-free的区别:
阻塞算法可能会出现整个系统都挂起的情况(占有锁的线程被中断,无法释放所,那么所有试图争用这个锁的线程会被挂起),系统中的所有线程全部饿死。
无锁算法可以保证系统中至少有一个线程处于工作状态,但是还是可能有线程永远抢不到资源而被饿死。
无等待算法保证系统中的所有线程都能处于工作状态,没有线程会被饿死,只要时间够,所有线程都能结束。相比于无锁算法,无等待算法有更强的保证。
一. 用互斥锁实现单生产者-单消费者
#include <string>
#include <sstream>
#include <list>
#include <pthread.h>
#include <iostream>
#include <time.h>
using namespace std;
int producer_count = 0;
int consumer_count = 0;
list<string> product;
list<string> consumer_list;
pthread_mutex_t mutex;
const int iterations = 10000;
//是否生产完毕标志
bool done = false;
void* producer(void* args)
{
for (int i = 0; i != iterations; ++i) {
pthread_mutex_lock(&mutex);
int value = ++producer_count;
stringstream ss;
ss<<value;
product.push_back(ss.str());
//cout<<"list push:"<<ss.str()<<endl;
pthread_mutex_unlock(&mutex);
}
return 0;
}
//消费函数
void* consumer(void* args)
{
//当没有生产完毕,则边消费边生产
while (!done) {
pthread_mutex_lock(&mutex);
if(!product.empty()){
consumer_list.splice(consumer_list.end(), product);
pthread_mutex_unlock(&mutex);
while(!consumer_list.empty()){
string value = consumer_list.front();
consumer_list.pop_front();
//cout<<"list pop:"<<value<<endl;
++consumer_count;
}
}else{
pthread_mutex_unlock(&mutex);
}
}
//如果生产完毕,则消费
while(!consumer_list.empty()){
string value = consumer_list.front();
consumer_list.pop_front();
//cout<<"list pop:"<<value<<endl;
++consumer_count;
}
return 0;
}
int main(int argc, char* argv[])
{
struct timespec time_start={0, 0},time_end={0, 0};
clock_gettime(CLOCK_REALTIME, &time_start);
pthread_t producer_tid;
pthread_t consumer_tid;
pthread_mutex_init (&mutex,NULL);
pthread_create(&producer_tid, NULL, producer, NULL);
pthread_create(&consumer_tid, NULL, consumer, NULL);
//等待生产者生产完毕
pthread_join(producer_tid, NULL);
//可以消费标志
done = true; //主线程不等生产线程完毕就设置done标记
cout << "producer done" << endl; //输出以观察主线程和各子线程的执行顺序
//等待消费者结束
pthread_join(consumer_tid, NULL);
clock_gettime(CLOCK_REALTIME, &time_end);
long cost = (time_end.tv_sec-time_start.tv_sec)/1000000 + (time_end.tv_nsec-time_start.tv_nsec)/1000;
cout<<"===========cost time:"<<cost<<"us==========="<<endl;
cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
}
生产消费10000个string类型的数据,耗时:58185us
二. Boost库的无锁队列
boost.lockfree实现了三种无锁数据结构:
boost::lockfree::queue
alock-free multi-produced/multi-consumer queue
一个无锁的多生产者/多消费者队列,注意,这个queue不支持string类型,支持的数据类型要求:
- T must have a copy constructor
- T must have a trivial assignment operator
- T must have a trivial destructor
boost::lockfree::stack
alock-free multi-produced/multi-consumer stack
一个无锁的多生产者/多消费者栈,支持的数据类型要求:
- T must have a copy constructor
boost::lockfree::spsc_queue
await-free single-producer/single-consumer queue (commonly known as ringbuffer)
一个无等待的单生产者/单消费者队列(通常被称为环形缓冲区),支持的数据类型要求:
- T must have a default constructor
- T must be copyable
详细资料可以看官方文档:http://www.boost.org/doc/libs/1_55_0/doc/html/lockfree.html
三. Queue示例
这里实现的还是单生产者-单消费者。
#include <pthread.h>
#include <boost/lockfree/queue.hpp>
#include <iostream>
#include <time.h>
#include <boost/atomic.hpp>
using namespace std;
//生产数量
boost::atomic_int producer_count(0);
//消费数量
boost::atomic_int consumer_count(0);
//队列
boost::lockfree::queue<int> queue(512);
//迭代次数
const int iterations = 10000;
//生产函数
void* producer(void* args)
{
for (int i = 0; i != iterations; ++i) {
int value = ++producer_count;
//原子计数————多线程不存在计数不上的情况
//若没有进入队列,则重复推送
while(!queue.push(value));
//cout<<"queue push:"<<value<<endl;
}
return 0;
}
//是否生产完毕标志
boost::atomic<bool> done (false);
//消费函数
void* consumer(void* args)
{
int value;
//当没有生产完毕,则边消费边生产
while (!done) {
//只要能弹出元素,就消费
while (queue.pop(value)) {
//cout<<"queue pop:"<<value<<endl;
++consumer_count;
}
}
//如果生产完毕,则消费
while (queue.pop(value)){
//cout<<"queue pop:"<<value<<endl;
++consumer_count;
}
return 0;
}
int main(int argc, char* argv[])
{
cout << "boost::lockfree::queue is ";
if (!queue.is_lock_free())
cout << "not ";
cout << "lockfree" << endl;
struct timespec time_start={0, 0},time_end={0, 0};
clock_gettime(CLOCK_REALTIME, &time_start);
pthread_t producer_tid;
pthread_t consumer_tid;
pthread_create(&producer_tid, NULL, producer, NULL);
pthread_create(&consumer_tid, NULL, consumer, NULL);
//等待生产者生产完毕
pthread_join(producer_tid, NULL);
//可以消费标志
done = true; //主线程不等生产线程完毕就设置done标记
cout << "producer done" << endl; //输出以观察主线程和各子线程的执行顺序
//等待消费者结束
pthread_join(consumer_tid, NULL);
clock_gettime(CLOCK_REALTIME, &time_end);
long cost = (time_end.tv_sec-time_start.tv_sec)/1000000 + (time_end.tv_nsec-time_start.tv_nsec)/1000;
cout<<"===========cost time:"<<cost<<"us==========="<<endl;
//输出生产和消费数量
cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
return 0;
}
生产消费10000个int类型的数据,耗时:3963us
stack与queue类似,只不过是先进后出。
四. Waitfree Single-Producer/Single-Consumer Queue无等待单生产者/单消费者队列
#include <pthread.h>
#include <iostream>
#include <time.h>
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/atomic.hpp>
using namespace std;
int producer_count = 0;
boost::atomic_int consumer_count (0);
boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024> > spsc_queue;
const int iterations = 10000;
void* producer(void* args)
{
for (int i = 0; i != iterations; ++i) {
int value = ++producer_count;
while(!spsc_queue.push(value));
//cout<<"queue push:"<<value<<endl;
}
return 0;
}
//是否生产完毕标志
boost::atomic<bool> done (false);
//消费函数
void* consumer(void* args)
{
int value;
//当没有生产完毕,则边消费边生产
while (!done) {
//只要能弹出元素,就消费
while (spsc_queue.pop(value)) {
//cout<<"queue pop:"<<value<<endl;
++consumer_count;
}
}
//如果生产完毕,则消费
while (spsc_queue.pop(value)){
//cout<<"queue pop:"<<value<<endl;
++consumer_count;
}
return 0;
}
int main(int argc, char* argv[])
{
using namespace std;
cout << "boost::lockfree::queue is ";
if (!spsc_queue.is_lock_free())
cout << "not ";
cout << "lockfree" << endl;
struct timespec time_start={0, 0},time_end={0, 0};
clock_gettime(CLOCK_REALTIME, &time_start);
pthread_t producer_tid;
pthread_t consumer_tid;
pthread_create(&producer_tid, NULL, producer, NULL);
pthread_create(&consumer_tid, NULL, consumer, NULL);
//等待生产者生产完毕
pthread_join(producer_tid, NULL);
//可以消费标志
done = true; //主线程不等生产线程完毕就设置done标记
cout << "producer done" << endl; //输出以观察主线程和各子线程的执行顺序
//等待消费者结束
pthread_join(consumer_tid, NULL);
clock_gettime(CLOCK_REALTIME, &time_end);
long cost = (time_end.tv_sec-time_start.tv_sec)/1000000 + (time_end.tv_nsec-time_start.tv_nsec)/1000;
cout<<"===========cost time:"<<cost<<"us==========="<<endl;
cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
}
生产消费10000个int类型的数据,耗时:1832us
如果把int改为string类型,耗时:28788us
五.性能对比
从上面可以看出在单生产者-单消费者模式下,spsc_queue比queue性能好,无锁队列比互斥锁的方式性能也要好。
Boost无锁队列的更多相关文章
- boost 无锁队列
一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...
- 基于Boost无锁队列实现的内存池
- 无锁队列以及ABA问题
队列是我们非常常用的数据结构,用来提供数据的写入和读取功能,而且通常在不同线程之间作为数据通信的桥梁.不过在将无锁队列的算法之前,需要先了解一下CAS(compare and swap)的原理.由于多 ...
- HashMap的原理与实 无锁队列的实现Java HashMap的死循环 red black tree
http://www.cnblogs.com/fornever/archive/2011/12/02/2270692.html https://zh.wikipedia.org/wiki/%E7%BA ...
- zeromq源码分析笔记之无锁队列ypipe_t(3)
在上一篇中说到了mailbox_t的底层实际上使用了管道ypipe_t来存储命令.而ypipe_t实质上是一个无锁队列,其底层使用了yqueue_t队列,ypipe_t是对yueue_t的再包装,所以 ...
- 一个可无限伸缩且无ABA问题的无锁队列
关于无锁队列,详细的介绍请参考陈硕先生的<无锁队列的实现>一文.然进一步,如何实现一个不限node数目即能够无限伸缩的无锁队列,即是本文的要旨. 无锁队列有两种实现形式,分别是数组与链表. ...
- 无锁队列--基于linuxkfifo实现
一直想写一个无锁队列,为了提高项目的背景效率. 有机会看到linux核心kfifo.h 原则. 所以这个实现自己仿照,眼下linux我们应该能够提供外部接口. #ifndef _NO_LOCK_QUE ...
- CAS简介和无锁队列的实现
Q:CAS的实现 A:gcc提供了两个函数 bool __sync_bool_compare_and_swap (type *ptr, type oldval, type newval, ...)// ...
- Go语言无锁队列组件的实现 (chan/interface/select)
1. 背景 go代码中要实现异步很简单,go funcName(). 但是进程需要控制协程数量在合理范围内,对应大批量任务可以使用"协程池 + 无锁队列"实现. 2. golang ...
随机推荐
- 自定义springboot-starter
参考: https://juejin.im/entry/5b447cbbe51d45199566f752 https://www.baeldung.com/spring-boot-custom-sta ...
- 创建LEANGOO项目
转自:https://www.leangoo.com/leangoo_guide/leangoo_guide_create_project.html#toggle-id-3 一个项目包括多个看板和多名 ...
- JavaJDBC【五、事务】
概念: 事务(Transaction)作为单个逻辑工作单元执行的一系列操作. 这些操作都是作为一个整体一起向系统提交,要么都执行,要么都不执行. 特点: 原子性:一个完整操作. 一致性:当事务完成时, ...
- java_day06_java高级特性
Advance Java Programming 第六章: java语言高级特性(part1) 1.static修饰符 1)static变量 在类中,使用static修饰的成员变量,就是静态变量,反之 ...
- java_day05_类和对象
chap05目标:类和对象---------------------------------------------- 1.OOP特征概述 Java的编程语言是面向对象的,采用这种语言进行编程称为面向 ...
- python面向编程:阶段练习
1.所有程序都因该使用面向对象来设计吗?为什么? 不是,面向对象编程优点是扩展性高,对程序员来说不需要关心具体的步骤,只需要调用对象功能,缺点是:程序的复杂度变高,整体的可控性比较低! 2.什么是对象 ...
- Thymeleaf初探
Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用 ...
- 需求分析&系统设计
这个作业属于哪个课程 课程链接 这个作业要求在哪里 作业要求 团队名称 朋友 代打了解一下 这个作业的目标 需求分析&系统设计 一.团队成员的姓名学号列表 学号 姓名 特长 061126 黄天 ...
- 白话跨域CORS
跨域访问控制是浏览器和服务器按照约定,协同工作,守护安全的一种机制. 其中认为浏览器和服务器是安全的,但是浏览器上运行的页面(HTML+JS)可能不安全. 分几种不同方式. 页面跨域简单请求(Get/ ...
- nginx基本概述
上级回顾: 1.NFS 2.Sersync 3.SSH 1.ssh使用两种登录服务器的方式,哪两种? 密码 用户名 + 密码 秘钥 用户名 + 秘钥(私钥) 公钥加密 私钥解密 2.大家常说的 塞ke ...