gpt4 book ai didi

c++ - Thread - 它是静态持续时间对象吗?

转载 作者:行者123 更新时间:2023-11-30 03:35:16 25 4
gpt4 key购买 nike

正如标题所说:std::thread 是静态持续时间对象吗?或者即使我没有显式访问静态变量,它也会使用一些吗?

编辑:

n2440 说(放弃进程):

The primary problem with destruction of static-duration objects is access to static-duration objects after their destructors have executed, thus resulting in undefined behavior. To prevent this problem, N2382 Dynamic Initialization and Destruction with Concurrency requires that all user threads finish before destruction begins (via a call to exit or return from main).

即使线程没有显式访问任何标记为 static 的变量,这是否成立?

最佳答案

std::thread 是一个类,它没有任何存储期。

std::thread 的对象(或实例)可以具有任何存储持续时间(静态(全局)、自动(在堆栈中)、动态(在堆中))。

n2440(放弃进程)关注全局对象被销毁后的访问。当 main 返回时可能会发生这种情况,但仍有额外的线程正在运行并访问全局对象(例如 std::cout),这些对象现在正被主线程销毁。

关于c++ - Thread - 它是静态持续时间对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41396527/

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