Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

应该是C++11规定 static local variable 的初始化是线程安全的和 copy assignment operator 漏写了字符 = #1

Closed
ltimaginea opened this issue Jan 6, 2022 · 1 comment
Assignees

Comments

@ltimaginea
Copy link

downdemo1

文件位置: content/02 线程间共享数据.md
https://github.com/downdemo/Cpp-Concurrency-in-Action-2ed/blob/cbd88caed525fffb736b4fe2fd3aa61a2d1f3bb2/content/02%20%E7%BA%BF%E7%A8%8B%E9%97%B4%E5%85%B1%E4%BA%AB%E6%95%B0%E6%8D%AE.md

Issue1

《C++ Concurrency in Action》Second Edition,Chapter 3.3.1 节最后部分关于 static变量 初始化的描述,仅指的是 static local variable 的初始化自 C++11 起是线程安全的,对于 static non-local variable 是未提及的,即对于具有静态存储期的非局部变量,如 global variable 和类的 static data member ,它们的初始化会作为程序启动的一部分在 main 函数的执行之前进行:简单的类型会在编译期进行初始化,如 constexpr 类型,复杂的类型则需要在运行期初始化,如 复杂的类对象(初始化时需要调用构造函数)。在程序结束时它们才会被销毁。因为 static non-local variable 的初始化会作为程序启动的一部分在 main 函数的执行之前进行,所以其初始化不存在多个线程竞争初始化的问题,但是却存在异常安全和 initialization order 的问题。详见 Static local variable and Global variable

即文中应该写为: “C++11规定static局部变量的初始化……”

Issue2

class A 的 copy assignment operator 漏写了字符 = ,即应该是: A& operator=(const A&) = delete;

@downdemo
Copy link
Owner

downdemo commented Jan 7, 2022

The following is the relevant information. You're accurate, and I will revise it soon.

C++11: N3242 6.7.4
C++14: N3797 6.7.4

The zero-initialization of all block-scope variables with static storage duration or thread storage duration is performed before any other initialization takes place. Constant initialization of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope. Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.

C++17: N4659 9.7.4
C++20: N4849 8.8.4

Dynamic initialization of a block-scope variable with static storage duration or thread storage duration is performed the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.

@downdemo downdemo self-assigned this Jan 7, 2022
@downdemo downdemo closed this as completed Jan 8, 2022
gitbook-com bot pushed a commit to gygzhang/Cpp-Concurrency-in-Action-2ed that referenced this issue Jul 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants