I am reviewing Facebook folly's lock free queue and am unfamililar with an approach they're using.
In the write() function, they use syntax
new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...);
I would have expected something more along the lines of
records_[currentWrite] = ....
I know im missing something, but if someone could kindly explain the use of new
here, as i would have thought that creating a heap variable would have an overhead consequence.
if (nextRecord != readIndex_.load(std::memory_order_acquire)) {
new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...);
writeIndex_.store(nextRecord, std::memory_order_release);
return true;
}
https://github.com/facebook/folly/blob/master/folly/ProducerConsumerQueue.h
Thanks!