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

<think>好的,我现在需要向用户介绍无锁队列。首先,我应该回忆一下无锁队列的基本概念,确保自己理解正确。无锁队列是一种并发数据结构,用于多线程环境,不需要使用传统的锁机制,比如互斥锁或信号量。这样可以避免死锁、优先级反转等问题,同时提高性能。 接下来,我需要确定无锁队列的核心特点。无锁(Lock-Free)通常指的是算法确保至少有一个线程能够继续执行,而不会因为其他线程的挂起或延迟而被阻塞。这和无阻塞(Wait-Free)不同,后者保证每个线程都能在有限步骤内完成操作。可能用户会混淆这两个概念,所以在介绍时需要明确区分。 然后,我应该考虑无锁队列的实现原理。常见的实现方式是利用原子操作,比如CAS(Compare-And-Swap)指令。例如,当多个线程尝试修改队列的头指针时,CAS可以确保只有一个线程成功更新指针,其他线程则重试。需要解释CAS的工作机制,以及它在无锁队列中的应用场景,比如入队和出队操作。 接下来,可能需要讨论无锁队列的优势。比如高并发下的性能优势,减少线程上下文切换的开销,避免死锁等。但同时也要提到其缺点,比如实现复杂度高,调试困难,以及可能出现的ABA问题。ABA问题是指在CAS操作中,一个值从A变成B又变回A,导致CAS误判为没有变化。解决ABA问题的方法通常包括使用带版本号的指针或双重CAS。 然后,我需要举一些实际应用场景,比如高性能计算、实时系统、游戏服务器等,这些场景中对低延迟和高吞吐量有较高要求,适合使用无锁队列。例如,任务调度、事件处理等。 还要考虑无锁队列的不同实现方式,比如基于数组的循环队列和基于链表的队列。基于数组的可能更简单,但容量固定;基于链表的动态扩展性好,但需要处理内存回收问题,比如使用危险指针(Hazard Pointers)或epoch-based回收机制。 此外,可能需要提到一些现有的无锁队列实现,比如Java中的ConcurrentLinkedQueue,C++的Boost库中的无锁数据结构,或者Intel TBB中的实现。这些可以作为用户进一步学习的参考。 在解释过程中,需要注意避免过于技术化的术语,或者用更通俗的语言进行说明。例如,CAS可以比喻为“检查并更新”的操作,只有当变量的值符合预期时才进行修改,否则重新尝试。 同时,需要提醒用户无锁队列并不总是最佳选择,需要根据具体场景权衡。例如,在低竞争情况下,锁的开销可能并不明显,而无锁实现的复杂度可能带来更多问题。 可能还需要提到一些常见的错误和挑战,比如正确管理内存回收,避免ABA问题,以及确保正确的内存序(Memory Order)在原子操作中的应用,防止指令重排导致的问题。 最后,总结无锁队列的适用场景、优缺点,以及使用时需要注意的事项,帮助用户全面理解何时以及如何正确使用无锁队列。</think>无锁队列是一种多线程环境下**无需传统锁机制**即可实现线程安全操作的并发数据结构,通过原子操作(如CAS)确保并发访问的正确性。以下从核心原理到应用场景分层说明: --- ### 一、核心特点 1. **无锁(Lock-Free)** 保证系统整体向前推进:即使某个线程被挂起,至少有一个其他线程能完成操作(非全阻塞)。 2. **非阻塞(Non-Blocking)** 线程通过重试而非休眠等待处理竞争,避免上下文切换开销。 3. **原子操作依赖** 依赖CPU提供的原子指令(如`CAS`、`LL/SC`)实现无锁同步。 --- ### 二、实现原理 #### 1. **CAS(Compare-And-Swap)指令** - 基本形式:`CAS(ptr, expected, new)` 若指针`ptr`指向的值等于`expected`,则将其更新为`new`,返回操作是否成功。 - **示例(入队操作伪代码)**: ```cpp void enqueue(Node* new_node) { Node* tail; do { tail = get_tail(); // 读取当前尾节点 new_node->next = tail->next; } while (!CAS(&tail->next, new_node, new_node)); // 尝试更新尾节点的next CAS(&global_tail, tail, new_node); // 更新全局尾指针 } ``` #### 2. **ABA问题与解决方案** - **问题**:线程1读取值A,线程2将A→B→A,线程1的CAS误判无变化。 - **解决**: - **版本号标记**:为指针附加版本号(如`std::atomic<T*>` + `uintptr_t`)。 - **危险指针(Hazard Pointers)**:延迟内存回收直至无线程访问。 --- ### 三、典型结构 #### 1. **链表式无锁队列** - **优点**:动态扩容,无固定容量限制。 - **挑战**:内存回收需谨慎(如通过RCU或Epoch-Based Reclamation)。 #### 2. **数组式循环队列** - **优点**:内存连续,缓存友好。 - **缺点**:固定容量,需处理队满冲突(如`CAS`移动头尾指针)。 --- ### 四、优缺点对比 | **优势** | **劣势** | |-----------------------------------|-----------------------------------| | 避免死锁/优先级反转 | 实现复杂度高,调试困难 | | 高并发性能(低竞争场景) | ABA问题需额外处理 | | 减少线程上下文切换 | 内存管理复杂(如节点回收) | --- ### 五、应用场景 1. **高性能计算**:任务队列调度(如线程池任务分发)。 2. **实时系统**:低延迟数据处理(金融交易订单匹配)。 3. **游戏服务器**:高并发事件处理(玩家动作同步)。 --- ### 六、代码示例(C++简版) ```cpp #include <atomic> template<typename T> class LockFreeQueue { struct Node { T value; std::atomic<Node*> next; Node(T val) : value(val), next(nullptr) {} }; std::atomic<Node*> head, tail; public: void enqueue(T val) { Node* new_node = new Node(val); Node* old_tail = tail.load(); while (true) { Node* null_ptr = nullptr; if (old_tail->next.compare_exchange_strong(null_ptr, new_node)) { tail.compare_exchange_strong(old_tail, new_node); // 更新tail break; } } } bool dequeue(T& result) { Node* old_head = head.load(); while (old_head != tail.load()) { if (head.compare_exchange_strong(old_head, old_head->next)) { result = old_head->value; delete old_head; // 需结合安全内存回收技术 return true; } } return false; } }; ``` --- ### 七、注意事项 1. **内存序(Memory Order)**: 明确原子操作的内存顺序(如`std::memory_order_relaxed` vs `std::memory_order_seq_cst`)。 2. **性能测试**: 高竞争场景可能表现不如锁+自旋锁组合,需实际压测验证。 3. **语言支持**: C++11+(`<atomic>`)、Java(`java.util.concurrent.atomic`)、Rust(`std::sync::atomic`)。 无锁队列是提升并发性能的利器,但需权衡复杂度与收益,建议优先使用成熟库(如`folly::AtomicLinkedList`或`Boost.Lockfree`)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值