c++标准是否保证用std::allocator构造不同的元素是线程安全的?例如:
// main thread
std::allocator<T> alloc;
auto *p = alloc.allocate(2);
// thread1
alloc.construct(p);
// thread2
alloc.construct(p + 1)
另外,std::allocator::destroy 对于不同的元素是线程安全的吗?
谢谢!
最佳答案
20.7.9.1 allocator members [allocator.members]
1 Except for the destructor, member functions of the default allocator shall not introduce data races (1.10) as a result of concurrent calls to those member functions from different threads. Calls to these functions that allocate or deallocate a particular unit of storage shall occur in a single total order, and each such deallocation call shall happen before the next allocation (if any) in this order.
是的,并发调用 construct
和 destroy
在 std::allocator<T>
保证是线程安全的。
关于c++ - 在构造不同的元素时, 'std::allocator<T>::construct' 是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36540378/