Whilst trying to work with std atomic pointer, I ran into the following. Say I do this:
std::atomic<std::string*> myString;
// <do fancy stuff with the string... also on other threads>
//A can I do this?
myString.load()->size()
//B can I do this?
char myFifthChar = *(myString.load()->c_str() + 5);
//C can I do this?
char myCharArray[255];
strcpy(myCharArray, myString.load()->c_str());
I'm pretty sure C is illegal because myString might be deleted in the meantime.
However I'm unsure about A and B. I suppose they are illegal since the pointer might be deferenced whilst performing the read operation.
However if this is the case, how can you ever read from an atomic pointer that might be deleted. Since the load is 1 step, and the reading of the data is 1 step.
Aucun commentaire:
Enregistrer un commentaire