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性能好,无锁队列比互斥锁的方式性能也要好。

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 无锁队列在多线程使用过程中可能会出现空的情况,这通常是由于多个线程同时访问队列的同一个节点造成的。 具体来说,如果一个线程正在执行出队操作,而另一个线程同时执行入队操作,它可能会覆盖正在出队的节点,导致出队的值为空。这种情况通常被称为ABA问题。 为了避免这种问题,可以使用一些技术来解决,例如使用带有版本号的指针来标识节点,或使用双向链表而不是单向链表。此外,可以使用一些现有的无锁队列实现,例如Boost的lockfree队列。 ### 回答2: 无锁队列在多线程使用过程中可能出现空的原因有以下几点: 1. 线程调度问题:多线程的执行是由操作系统进行调度的,当多个线程同时竞争对无锁队列进行操作时,可能会出现某个线程被暂时挂起,导致无锁队列中的元素没有及时被处理,从而出现空的情况。 2. 内存模型问题:无锁队列中元素的读写操作可能存在内存可见性问题。当一个线程将元素写入队列时,可能由于缓存一致性等问题,其他线程无法立即看到该元素,从而导致其他线程在读取队列时出现空的情况。 3. 并发冲突问题:多个线程同时对无锁队列进行操作时,可能会出现并发冲突问题,例如多个线程同时进行插入或删除操作,可能会导致元素丢失或者被重复删除,从而引发空的情况。 针对这些问题,可以采取以下措施来避免无锁队列出现空的情况: 1. 检查竞态条件:在进行无锁队列的插入和删除操作时,需要仔细检查相关的竞态条件,并采取合适的同步措施,例如使用原子操作或者特定的同步指令,保证操作的原子性和正确性。 2. 使用内存屏障:在无锁队列的读写操作中,可以通过使用内存屏障等指令,来确保线程之间的内存可见性,从而避免元素读取时出现空的情况。 3. 合理的调度策略:在多线程环境下,需要合理安排线程的执行顺序和调度策略,尽量减少多个线程竞争无锁队列的情况,从而降低出现空的概率。 4. 使用适当的数据结构:如果无锁队列在多线程环境下出现频繁的空情况,可以考虑使用一些更加适合并发环境的数据结构,例如无锁链表或者无锁哈希表,来提高并发性能并减少空的情况的发生。 ### 回答3: 无锁队列在多线程使用过程中出现空可能有以下几种情况: 1. 生产者速度过快:如果生产者的生产速度远远快于消费者的消费速度,就会导致队列中的数据无法及时被消费掉,从而导致队列出现空的情况。 2. 消费者速度过快:与生产者速度过快相反,如果消费者的消费速度远远快于生产者的生产速度,就会导致队列中的数据被迅速消费完毕,从而导致队列出现空的情况。 3. 线程竞争:在使用无锁队列时,多个线程同时对队列进行操作,如果竞争过于激烈,可能导致某些线程在执行过程中无法完成操作,从而导致队列出现空的情况。 4. 非原子操作:如果在操作无锁队列时,没有正确使用原子操作或者未对操作进行适当的同步控制,可能会导致一些数据的丢失或者错误的操作序列,从而导致队列出现空的情况。 为了避免无锁队列为空的情况,可以采取以下措施: 1. 增加消费者线程的数量:如果发现消费者无法及时消费队列中的数据,可以增加消费者线程的数量,以提高队列中数据被消费的速度。 2. 增加生产者线程的数量:如果发现生产者过快导致队列为空的情况,可以增加生产者线程的数量,以提高数据的生产速度,使其能够满足消费者的需求。 3. 合理调整线程的优先级:根据实际情况,可以根据消费者和生产者的重要程度和需求来合理调整线程的优先级,以保证队列中的数据能够得到及时处理。 4. 增加缓冲区大小:如果发现队列空的情况多发生在生产者过快导致消费者无法及时消费的情况下,可以适当增大无锁队列的缓冲区大小,以增加队列中能够存放的数据量,从而减少队列为空的情况的发生。 综上所述,要避免无锁队列在多线程使用过程中出现空的情况,需要根据具体情况采取相应的措施,并保证正确使用原子操作和适当的同步控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值