代码拉取完成,页面将自动刷新
// day15-threadsafehash.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include "threadsafetable.h"
#include "myclass.h"
#include <thread>
#include <set>
void TestThreadSafeHash() {
std::set<int> removeSet;
threadsafe_lookup_table<int, std::shared_ptr<MyClass>> table;
std::thread t1([&]() {
for(int i = 0; i < 100; i++)
{
auto class_ptr = std::make_shared<MyClass>(i);
table.add_or_update_mapping(i, class_ptr);
}
});
std::thread t2([&]() {
for (int i = 0; i < 100; )
{
auto find_res = table.value_for(i, nullptr);
if(find_res)
{
table.remove_mapping(i);
removeSet.insert(i);
i++;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
});
std::thread t3([&]() {
for (int i = 100; i < 200; i++)
{
auto class_ptr = std::make_shared<MyClass>(i);
table.add_or_update_mapping(i, class_ptr);
}
});
t1.join();
t2.join();
t3.join();
for(auto & i : removeSet)
{
std::cout << "remove data is " << i << std::endl;
}
auto copy_map = table.get_map();
for(auto & i : copy_map)
{
std::cout << "copy data is " << *(i.second) << std::endl;
}
}
int main()
{
TestThreadSafeHash();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。