在多线程环境中交换 c++ 映射对象
swap c++ map objects in multithreaded environment
我是 c++ 编码的新手,需要 swap/replace 旧的 multimap 对象与新构建的 multimap 对象,因为这个对象将被缓存我想在构建之后替换现有对象新对象,只需替换对象本身。这将在多线程环境中使用,因此使用原子加载。如本帖 Want an efficient way to swap two pointers in C++ 所述。我写了这段代码
#include<iostream>
#include<map>
#include<atomic>
#include<string>
using namespace std;
// MultiMap Object
struct mmap{
multimap<string,int> stringTointmap;
};
// Structure to swap two instances of multimap
struct swapMap{
mmap* m1;
mmap* m2;
};
int main(){
//create Two Objects
mmap* old = new mmap();
mmap* new2= new mmap();
// populate first object
old->stringTointmap.insert(make_pair("old",1));
//populate second object
new2->stringTointmap.insert(make_pair("new1",2));
//swap two objects
atomic<swapMap> swap;
auto refresh=swap.load();
refresh= {swap.m2,swap.m1};
}
但是我遇到了这个错误
error: expected expression
refresh= {swap.m2,swap.m1};
当然,我遗漏了什么,有人可以帮忙吗?
下面的示例代码展示了如何在 std::shared_ptr
上使用原子操作。
#include <memory>
#include <thread>
#include <chrono>
#include <atomic>
#include <iostream>
std::shared_ptr<std::string> the_string;
int main()
{
std::atomic_store(&the_string, std::make_shared<std::string>("first string"));
std::thread thread(
[&](){
for (int i = 0; i < 5; ++i)
{
{
// pin the current instance in memory so we can access it
std::shared_ptr<std::string> s = std::atomic_load(&the_string);
// access it
std::cout << *s << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
std::this_thread::sleep_for(std::chrono::seconds(2));
// replace the current instance with a new instance allowing the old instance
// to be removed when all threads are done with it
std::atomic_store (&the_string, std::make_shared<std::string>("second string"));
thread.join();
}
输出:
first string
first string
second string
second string
second string
我是 c++ 编码的新手,需要 swap/replace 旧的 multimap 对象与新构建的 multimap 对象,因为这个对象将被缓存我想在构建之后替换现有对象新对象,只需替换对象本身。这将在多线程环境中使用,因此使用原子加载。如本帖 Want an efficient way to swap two pointers in C++ 所述。我写了这段代码
#include<iostream>
#include<map>
#include<atomic>
#include<string>
using namespace std;
// MultiMap Object
struct mmap{
multimap<string,int> stringTointmap;
};
// Structure to swap two instances of multimap
struct swapMap{
mmap* m1;
mmap* m2;
};
int main(){
//create Two Objects
mmap* old = new mmap();
mmap* new2= new mmap();
// populate first object
old->stringTointmap.insert(make_pair("old",1));
//populate second object
new2->stringTointmap.insert(make_pair("new1",2));
//swap two objects
atomic<swapMap> swap;
auto refresh=swap.load();
refresh= {swap.m2,swap.m1};
}
但是我遇到了这个错误
error: expected expression
refresh= {swap.m2,swap.m1};
当然,我遗漏了什么,有人可以帮忙吗?
下面的示例代码展示了如何在 std::shared_ptr
上使用原子操作。
#include <memory>
#include <thread>
#include <chrono>
#include <atomic>
#include <iostream>
std::shared_ptr<std::string> the_string;
int main()
{
std::atomic_store(&the_string, std::make_shared<std::string>("first string"));
std::thread thread(
[&](){
for (int i = 0; i < 5; ++i)
{
{
// pin the current instance in memory so we can access it
std::shared_ptr<std::string> s = std::atomic_load(&the_string);
// access it
std::cout << *s << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
std::this_thread::sleep_for(std::chrono::seconds(2));
// replace the current instance with a new instance allowing the old instance
// to be removed when all threads are done with it
std::atomic_store (&the_string, std::make_shared<std::string>("second string"));
thread.join();
}
输出:
first string
first string
second string
second string
second string