jeudi 26 février 2015

Atomic design with unordered_map thread safe?


i have the following hierarchy:



struct Point
{
std::atomic<double> x;
std::atomic<double> y;
std::atomic<double> z;
}

class Data
{
std::atomic<int> version;
Point point;
}

unordered_map<std::string,Data> hashtable; // global


Thread1 is the producer:


Thread 1 receive a std::pair<std::string,Point> new_data, it does:



shared_hashtable[new_data.first].point.x.store(new_data.second.x,memory_order_release) // update X
shared_hashtable[new_data.first].point.y.store(new_data.second.y,memory_order_release) // update Y
shared_hashtable[new_data.first].point.z.store(new_data.second.z,memory_order_release) // update Z
hashtable[new_data.first].version.store(hashtable[new_data.first].version+1,memory_order_release) // update the version of the data


Thread 2 does:



int local_version;
while(local_version!=shared_hashtable["string1"].version.load(memory_order_acquire))
{
//...process data...
local_version=hared_hashtable["string1"].version.load(memory_order_acquire);
}


My question is: does the memory order passed guarantee that my store and load wont be reordered. Does the design work as expected : if the object at hastable["string1"] is updated, will I process the corresponding data in thread 2 ?




Aucun commentaire:

Enregistrer un commentaire