這是我編寫的併發隊列,我打算在我寫的線程池中使用。我想知道是否有任何性能改進。如果您好奇,atomic_counter
被粘貼在下面!批評我的併發隊列
#ifndef NS_CONCURRENT_QUEUE_HPP_INCLUDED
#define NS_CONCURRENT_QUEUE_HPP_INCLUDED
#include <ns/atomic_counter.hpp>
#include <boost/noncopyable.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#include <cassert>
#include <cstddef>
namespace ns {
template<typename T,
typename mutex_type = boost::detail::spinlock,
typename scoped_lock_type = typename mutex_type::scoped_lock>
class concurrent_queue : boost::noncopyable {
struct node {
node * link;
T const value;
explicit node(T const & source) : link(0), value(source) { }
};
node * m_front;
node * m_back;
atomic_counter m_counter;
mutex_type m_mutex;
public:
// types
typedef T value_type;
// construction
concurrent_queue() : m_front(0), m_mutex() { }
~concurrent_queue() { clear(); }
// capacity
std::size_t size() const { return m_counter; }
bool empty() const { return (m_counter == 0); }
// modifiers
void push(T const & source);
bool try_pop(T & destination);
void clear();
};
template<typename T, typename mutex_type, typename scoped_lock_type>
void concurrent_queue<T, mutex_type, scoped_lock_type>::push(T const & source) {
node * hold = new node(source);
scoped_lock_type lock(m_mutex);
if (empty())
m_front = hold;
else
m_back->link = hold;
m_back = hold;
++m_counter;
}
template<typename T, typename mutex_type, typename scoped_lock_type>
bool concurrent_queue<T, mutex_type, scoped_lock_type>::try_pop(T & destination) {
node const * hold;
{
scoped_lock_type lock(m_mutex);
if (empty())
return false;
hold = m_front;
if (m_front == m_back)
m_front = m_back = 0;
else
m_front = m_front->link;
--m_counter;
}
destination = hold->value;
delete hold;
return true;
}
template<typename T, typename mutex_type, typename scoped_lock_type>
void concurrent_queue<T, mutex_type, scoped_lock_type>::clear() {
node * hold;
{
scoped_lock_type lock(m_mutex);
hold = m_front;
m_front = 0;
m_back = 0;
m_counter = 0;
}
if (hold == 0)
return;
node * it;
while (hold != 0) {
it = hold;
hold = hold->link;
delete it;
}
}
}
#endif
atomic_counter.hpp
#ifndef NS_ATOMIC_COUNTER_HPP_INCLUDED
#define NS_ATOMIC_COUNTER_HPP_INCLUDED
#include <boost/interprocess/detail/atomic.hpp>
#include <boost/noncopyable.hpp>
namespace ns {
class atomic_counter : boost::noncopyable {
volatile boost::uint32_t m_count;
public:
explicit atomic_counter(boost::uint32_t value = 0) : m_count(value) { }
operator boost::uint32_t() const {
return boost::interprocess::detail::atomic_read32(const_cast<volatile boost::uint32_t *>(&m_count));
}
void operator=(boost::uint32_t value) {
boost::interprocess::detail::atomic_write32(&m_count, value);
}
void operator++() {
boost::interprocess::detail::atomic_inc32(&m_count);
}
void operator--() {
boost::interprocess::detail::atomic_dec32(&m_count);
}
};
}
#endif
您有具體的問題要問,還是需要解決的地方? SO不是代碼評論網站。 – 2010-07-07 05:08:12
我做了編輯,表明我對提高性能的建議感興趣。 – 2010-07-07 05:13:33
你見過香草薩特的DDJ文章(http://www.drdobbs.com/cpp/211601363)嗎?他討論了一些性能改進(對push和pop使用不同的鎖,對緩存行大小使用padding成員變量)。更一般地說,爲什麼你實現自己的而不是使用,例如TBB的'concurrent_queue'? – stephan 2010-07-07 07:05:03