Boost logo

Boost Users :

Subject: [Boost-users] Using Boost Mutex
From: Wesley Bland (wesbland_at_[hidden])
Date: 2009-09-18 13:21:39


This is firmly in n00b territory.
I'm trying to use a boost::mutex to lock a STL queue. The mutex is stored
in a class in a map. When I pass the mutex to the boost::mutex::scoped_lock
constructor, I get this error:

/usr/local/include/boost/noncopyable.hpp: In copy constructor
‘boost::mutex::mutex(const boost::mutex&)’:
/usr/local/include/boost/thread/pthread/mutex.hpp:31: instantiated from
‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with
_Key = int, _Tp = boost::mutex, _Compare = std::less<int>, _Alloc =
std::allocator<std::pair<const int, boost::mutex> >]’
SwitchBox.cpp:15: instantiated from here
/usr/local/include/boost/noncopyable.hpp:27: error:
‘boost::noncopyable_::noncopyable::noncopyable(const
boost::noncopyable_::noncopyable&)’ is private
/usr/local/include/boost/thread/pthread/mutex.hpp:31: error: within this
context
/usr/include/c++/4.2.1/bits/stl_map.h: In member function ‘_Tp&
std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key =
int, _Tp = boost::mutex, _Compare = std::less<int>, _Alloc =
std::allocator<std::pair<const int, boost::mutex> >]’:
/usr/include/c++/4.2.1/bits/stl_map.h:350: note: synthesized method
‘boost::mutex::mutex(const boost::mutex&)’ first required here

This appears to be complaining about me passing the mutex into the
scoped_lock. Can anyone give me some help here? I've included a much
shortened version of the code that otherwise compiles.

Thanks for your help,
Wesley

SwitchBox.hpp
-----

#include <map>
#include <queue>
#include <boost/thread/mutex.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;
using namespace std;

namespace Network {
    typedef struct Message {
int type;
 unsigned long sender;
unsigned long destination;
int size;
 void *body;
char *PackHeader();
void UnpackHeader(char *);
 } Message;
    class SwitchBox {
    protected:
        unsigned long myID;
        int headerSize;
        map< int, queue< Message > > sendQueues;
        map< int, boost::mutex > sendMutexes;

        bool Initialize();
        void SendThread(unsigned long id, tcp::socket sock);
    };
}

SwitchBox.cpp
------

#include "Switchbox.hpp"

using namespace Network;

bool SwitchBox::Initialize() {
myID = 0;
 headerSize = sizeof(int) + sizeof(unsigned long) * 2 + sizeof(int);
 return true;
}

void SwitchBox::SendThread(unsigned long id, tcp::socket sock) {
// Set up the lock so it can be locked and unlocked as we need
 boost::mutex::scoped_lock sendLock(sendMutexes[id], boost::defer_lock);
 while (true) {
 // First get the message off of the message queue
 // Lock the message queue so nothing is moved unexpectedly
 sendLock.lock();
 Message m = sendQueues[id].front();
 sendQueues[id].pop();
 // Unlock the message queue so everyone else can get to it now
 sendLock.unlock();
 // Next send the message through the socket
 boost::asio::write(sock, boost::asio::buffer(m.PackHeader(), headerSize));
boost::asio::write(sock, boost::asio::buffer((char *) m.body, m.size));
 }
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net