我正在审查 Facebook 愚蠢的无锁队列,但不熟悉他们使用的方法。
在 write() 函数中,它们使用语法
new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...);
我会期待更多类似的东西
records_[currentWrite] = ....
我知道我错过了一些东西,但是如果有人可以解释一下new
这里的用法,因为我认为创建堆变量会产生开销后果。
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
谢谢!