mercredi 25 février 2015

Is this lock free design thread safe?


In different threads I do the following:


shared variable:



std::shared_ptr<Data> dataPtr;
std::atomic<int> version_number;


Thread1, the producer receive new data and do



dataPtr.reset(newdata);
version_number++;


Other threads, consumers are doing:



int local_version=0;
std::shared_ptr<Data> localPtr;
while(local_version!=version_number)
localPtr=dataPtr;
...operation on my_ptr...
localPtr.reset();
local_version=version_number.load();


Here I know that the consumers might skip some version, if they are processing data and new updates keep going, thats fine by me, i dont need them to process all versions, just the last available to them. My question is, is this line atomic :



localPtr=dataPtr;


Will I always obtain the last version of what is in dataPtr or will this be cached or might lead to anything wrong in my design ?


Thks.




Aucun commentaire:

Enregistrer un commentaire