lapots
lapots

Reputation: 13415

using boost ptr_unordered_map

I've got a class ItemThreadingController with boost::ptr_unordered_map where key is id of the thread, and value is a boost::thread with some task. I want to add and erase threads. Basically it's a primitive thread manager.

The question is - what is the correct way to use ptr_unordered_map - to declare and to get access to its elements? When I try like this

class ItemThreadingController
{
private:
boost::ptr_unordered_map<int, boost::thread> controllerMap;
long int id;
public:
ItemThreadingController() 
{
    id = -1;
}
int AddThread(void (*func)())
{
    boost::thread t(func);
    id++;
    controllerMap.insert(std::make_pair(id, t));
    return id;
}
void StopThread(long int id)
{
    controllerMap[id].interrupt();
    controllerMap[id].join();
    controllerMap.erase(id);
    cout<<"Thread "<<id<<"has stopped and erased!"<<endl;
}
~ItemThreadingController() {};
};

I've got several errors

error C2664: 'void boost::ptr_map_adapter<T,VoidPtrMap,CloneAllocator,Ordered>::insert<boost::type>(InputIterator,InputIterator)' : cannot convert parameter 1 from 'boost::type' to 'boost::type'  518
error C2664: 'boost::ptr_map_iterator<I,F,S> boost::ptr_container_detail::associative_ptr_container<Config,CloneAllocator>::erase(boost::ptr_map_iterator<I,F,S>,boost::ptr_map_iterator<I,F,S>)' : cannot convert parameter 1 from 'boost::type' to 'boost::ptr_map_iterator<I,F,S>'   185
error C2039: 'type' : is not a member of 'boost::mpl::eval_if_c<C,F1,F2>'   63
error C2039: 'type' : is not a member of 'boost::mpl::eval_if_c<C,F1,F2>'   63

How to use it properly?

Upvotes: 0

Views: 613

Answers (1)

Johan
Johan

Reputation: 3778

boost::ptr_unordered_map<A, B> can be seen as an equivalent to std::unordered_map<A, std::unique_ptr<B>>. It aims to simplify the memory management on deletion and element removal.

When you're doing this:

boost::thread t(func);
id++;
controllerMap.insert(std::make_pair(id, t));

It cannot work the map expects a pointer as pair::second.

Moreover, I am not sure boost::ptr_XXX mimics exactly std::XXXX and I do not think there is a boost::ptr_unordered_map<A, B>::insert() overload taking a std::pair<A, B*>. But at least you have an overload boost::ptr_unoreder_map<A, B>::insert(A&, B*);.

Upvotes: 1

Related Questions