2
#include <vector>
#include <string>
#include <mutex>
#include <future>
using namespace std;
mutex g_mtx;
vector<string> g_coll;
void Cleaner()
{
lock_guard<mutex> lock(g_mtx);
g_coll.clear();
}
const vector<string>& Getter()
{
lock_guard<mutex> lock(g_mtx);
return g_coll;
}
int main()
{
g_coll = { "hello" };
auto fut = async([&]()
{
Cleaner();
});
auto returned_coll = Getter();
fut.get();
}
如果Cleaner
被return g_coll;
後執行,不C++標準保證是returned_coll
包含{ "hello" }
?以「std :: lock_guard <mutex>」作爲參考返回共享對象是否安全?