0
std::pair<Object, OtherObject*> currentData;

void OnCallback()
{
    Object object = getObject();
    OtherObject* otherObject = new OtherObject();
    currentData = std::make_pair(object, otherObject);
}

Is make_pair atomic? Will make_pair copy or move the fields from its return value to currentData? If I have another thread accessing the value of currentData, is there any potential that currentData's value will be incomplete when it's accessed?

Unfortunately I didn't see any related information in the standard docs for make_pair.

Xander Dunn
  • 2,349
  • 2
  • 20
  • 32
  • 1
    Can you explain what you mean by "atomic"? Because it's obviously not atomic in the sense of `std::atomic`. – Brian Bi May 03 '16 at 00:06
  • 1
    `std::make_pair` uses move-semantics, however that does not mean it is atomic in compliance with `std::atomic` – sjrowlinson May 03 '16 at 00:08
  • 2
    If not in the sense of `std::atomic` then in what sense do you mean. Because your quoted sense seems to be what `std::atomic` does. – Martin York May 03 '16 at 00:16
  • @LokiAstari I think my entire question boils down to the atomicity of the assignment operator. This helped me: http://stackoverflow.com/questions/8919818/is-pointer-assignment-atomic-in-c I didn't even bother looking at the `std::atomic` class, but it looks like it's what I want. Also this helped me: http://stackoverflow.com/questions/16320838/when-do-i-really-need-to-use-atomicbool-instead-of-bool – Xander Dunn May 03 '16 at 00:29

1 Answers1

5

Whether make_pair() is or isn't atomic is irrelevant.

If I have another thread accessing the value of currentData, is there any potential that currentData's value will be incomplete when it's accessed?

The only question here is whether std::pair's assignment operator is atomic, since that's what determines whether the assignment operation is thread-safe or not. What happened before the assignment took place, and how the value that's being assigned came into existence, bears absolutely no relevance on the atomicity of the assignment operation.

This assignment operation is not atomic, and is not thread-safe.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148