高性能并发队列(C++实现)
更新于2014-05-04 19:34:13
算法参考:http://www.parallellabs.com/2010/10/25/practical-concurrent-queue-algorithm/,非常适合生产者-消费者模型。
注意:1、析构函数没有加锁,因为需要同时对head lock和tail lock加锁。不建议在析构不确定的情况下使用。
2、经测试,比加锁的std::list快50%,比加锁的std::queue慢20%。
-
template <
typename T>
-
class concurrent_queue
-
{
-
public:
-
concurrent_queue()
-
{
-
NODE* node =
new NODE();
-
node->next =
NULL;
-
-
head_ = node;
-
tail_ = node;
-
}
-
-
~concurrent_queue()
-
{
-
NODE* node = head_;
-
-
do
-
{
-
node = erase_(node);
-
}
-
while(node !=
NULL);
-
}
-
-
void push(const T& val)
-
{
-
NODE* node =
new NODE();
-
node->val = val;
-
node->next =
NULL;
-
-
scoped_lock lock(t_lock_);
-
tail_->next = node;
-
tail_ = node;
-
}
-
-
bool pop(T* val)
-
{
-
scoped_lock lock(h_lock_);
-
if(empty_())
-
{
-
return
false;
-
}
-
-
head_ = erase_(head_);
-
*val = head_->val;
-
return
true;
-
}
-
-
private:
-
struct NODE
-
{
-
T val;
-
NODE* next;
-
};
-
-
private:
-
bool empty_() const
-
{
-
return head_->next ==
NULL;
-
}
-
-
NODE* erase_(NODE* node) const
-
{
-
NODE* tmp = node->next;
-
delete node;
-
return tmp;
-
}
-
-
private:
-
NODE* head_;
-
NODE* tail_;
-
concurrent_lock h_lock_;
-
concurrent_lock t_lock_;
-
};
-
class concurrent_lock
-
{
-
public:
-
concurrent_lock()
-
{
-
InitializeCriticalSection(&cs_);
-
}
-
-
~concurrent_lock()
-
{
-
DeleteCriticalSection(&cs_);
-
}
-
-
void lock()
-
{
-
EnterCriticalSection(&cs_);
-
}
-
-
void unlock()
-
{
-
LeaveCriticalSection(&cs_);
-
}
-
-
private:
-
CRITICAL_SECTION cs_;
-
};
-
-
class scoped_lock
-
{
-
public:
-
scoped_lock(
const concurrent_lock& lock) : lock_(lock)
-
{
-
const_cast<concurrent_lock&>(lock_).lock();
-
}
-
-
~scoped_lock()
-
{
-
const_cast<concurrent_lock&>(lock_).unlock();
-
}
-
-
private:
-
const concurrent_lock& lock_;
-
};
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">3</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.youkuaiyun.com/fdsdfdsf"> <img src="https://profile.csdnimg.cn/E/0/7/3_fdsdfdsf" class="avatar_pic" username="fdsdfdsf"> <img src="https://g.csdnimg.cn/static/user-reg-year/1x/10.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.youkuaiyun.com/fdsdfdsf" data-report-click="{"mod":"popu_379"}" target="_blank">fdsdfdsf</a></span> </div> <div class="text"><span>发布了14 篇原创文章</span> · <span>获赞 2</span> · <span>访问量 2万+</span></div> </div> <div class="right-message"> <a href="https://im.youkuaiyun.com/im/main.html?userName=fdsdfdsf" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div> </article>
本文介绍了一种基于C++的高性能并发队列实现方法,适用于生产者-消费者模型,通过对比测试,该队列在性能上优于加锁的std::list,但略逊于加锁的std::queue。
6714

被折叠的 条评论
为什么被折叠?



