gpt4 book ai didi

c++ - 在不同的线程中使用 map::empty 和 map::insert

转载 作者:搜寻专家 更新时间:2023-10-31 00:27:26 26 4
gpt4 key购买 nike

<分区>

在没有同步的情况下在 2 个不同的线程中使用 map::empty 和 map::insert 是否安全?

void thread1()
{
...
while(running)
{
//get data from network
..........

std::lock_guard<std::mutex> lock(mtx);
map1.insert(data);
}
}

void thread2()
{
while(running || !map1.empty())
{
std::lock_guard<std::mutex> lock(mtx);
//remove outdated elements from map
......
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com