c++ - 在构造不同的元素时, 'std::allocator<T>::construct' 是线程安全的吗?

标签 c++ multithreading c++11

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.

是的,并发调用 constructdestroystd::allocator<T>保证是线程安全的。

关于c++ - 在构造不同的元素时, 'std::allocator<T>::construct' 是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36540378/

相关文章:

c++ - Silverlight 和 C++ 或 C++ 到 C# 正在进行的代码转换?

java - 如何在不干扰程序 UI 的情况下播放声音?

java - 遍历通过 ThreadPoolTask​​Executor 运行的线程

c++ - 如何初始化数组并将其作为参数传递而不声明它?

c++ - 当我在子类中具有相同名称的函数时,如何调用基类中定义的派生 const 函数?

c - 为什么在 select 中使用 writefds ?如何在实践中使用它们?

c++ - 检查子类是否执行了方法重写

windows - 创建命名管道 C++ Windows

c++ - 提高 std::vector 的存储容量/性能

c++ - Windows WMI - Win32_ProcessStartTrace 和 __InstanceCreationEvent 之间的区别(和顺序)是什么?