Dmitry
Dmitry

Reputation: 2158

Thread safe copyable circular buffer in boost

I'm using boost 1.53 and I'd like to have a thread safe implementation of circular buffer inside my app. Please have a look at the app concept below :

I have N sockets from where I read data and put it to the respective circular buffer. The point is that I don't know the exact number of sockets to be opened ( they could be opened even dynamically ) One obvious solution appeared in my mind is to have a map between and inside my class. But it's not possible to achieve as mutex is not a copyable object.

What is the best solution for my task? Any ideas would be appreciated. Looking forward to your replies.

Tnx in advance, Dmitry

Upvotes: 0

Views: 935

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

But it's not possible to achieve as mutex is not a copyable object.

Which is not a problem, since you don't want the copy's mutex to have any relationship to the original's mutex.

You can just copy the data as you would for a normal map and then default construct a new mutex for the copy.

class MyBuffer
{
    std::map<key_T, value_T> m_data;
    mutable std::mutex m_mutex;
public:
    MyBuffer(MyBuffer const& other)
    {
        std::lock_guard<std::mutex> lk(other.m_mutex);
        m_data = other.m_data;
    }
    // [...]
};

Upvotes: 1

Related Questions