This is an example of how to use the concurrent_map class with Metall.
#include <iostream>
#include <thread>
char, int, std::less<char>, std::hash<char>,
bool insert_func1(const char key, const int value, map_type *pmap) {
return pmap->insert(std::make_pair(key, value));
}
bool insert_func2(const char key, const int value, map_type *pmap) {
auto val = std::make_pair(key, value);
return pmap->insert(std::move(val));
}
bool insert_func3(const char key, const int value, map_type *pmap) {
const auto const_val = std::make_pair(key, value);
return pmap->insert(const_val);
}
void scoped_edit(const char key, const int value, map_type *pmap) {
auto ret = pmap->scoped_edit(key);
ret.first = value;
}
void edit(const char key, const int value, map_type *pmap) {
pmap->edit(key, [&value](int &mapped_value) {
mapped_value = value;
});
}
{
auto pmap =
manager.construct<map_type>(
"map")(
manager.get_allocator<>());
{
std::thread t1(insert_func1, 'a', 0, pmap);
std::thread t2(insert_func2, 'b', 1, pmap);
std::thread t3(insert_func3, 'c', 2, pmap);
t1.join();
t2.join();
t3.join();
}
{
std::thread t1(scoped_edit, 'a', 10, pmap);
std::thread t2(edit, 'b', 20, pmap);
t1.join();
t2.join();
}
}
{
auto pmap =
manager.find<map_type>(
"map").first;
edit('c', 30, pmap);
for (auto itr = pmap->cbegin(), end = pmap->cend(); itr != end; ++itr) {
std::cout << itr->first << " " << itr->second << std::endl;
}
}
return 0;
}
int main()
Definition: jgraph.cpp:24