gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:36:58 27 4
gpt4 key购买 nike

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 functionsthat allocate or deallocate a particular unit of storage shall occur in a single total order, and each suchdeallocation 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com