- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的代码应该初始化一个静态线程局部
和静态
结构。
#include <iostream>
struct Tracer {
public:
Tracer(const char *new_name) : name{new_name} {
printf("%s : Constructor()\n", this->name);
}
~Tracer() {
printf("%s : Destructor()\n", this->name);
}
private:
const char *name;
};
// 1. Thread-Local Storage Duration
static thread_local Tracer t_thread_local{"Thread-Local Storage Duration"};
// 2. Static Storage Duration
static Tracer t_static{"Static Storage Duration"};
int main() {
printf("Start Program\n");
}
但是,我没有看到来自静态线程本地
结构构造函数/析构函数的预期消息。打印的输出仅显示来自 static
结构的消息。我错过了什么吗?
Static Storage Duration : Constructor()
Start Program
Static Storage Duration : Destructor()
最佳答案
您认为仅声明和定义这些对象就一定会触发预main
构造的假设是不正确的。
[basic.stc.thread/2]
: [ Note: A variable with thread storage duration is initialized as specified in [basic.start.static], [basic.start.dynamic], and [stmt.dcl] and, if constructed, is destroyed on thread exit ([basic.start.term]). — end note ]
[basic.start.dynamic/5]
: It is implementation-defined whether the dynamic initialization of a non-local non-inline variable with static storage duration is sequenced before the first statement of main or is deferred. If it is deferred, it strongly happens before any non-initialization odr-use of any non-inline function or non-inline variable defined in the same translation unit as the variable to be initialized. [..]
实际上 basic.start.dynamic
中有许多类似的规则。
重点是,您的程序不会做太多事情,而且它肯定不会使用 t_thread_local
,因此由编译器决定 t_thread_local
是否会使用确实存在,如果存在,何时存在。
关于c++ - 未调用已初始化静态 thread_local 结构的构造函数和析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59539536/
我想知道以下两个声明之间的区别是什么,如果两者都写在一个头文件中: inline thread_local MyClass obj1; // inline with thread_local thr
“所有具有线程局部存储持续时间的非局部变量都作为线程启动的一部分进行初始化,在线程函数开始执行之前进行排序。” ( https://en.cppreference.com/w/cpp/language
我在文件 tracker.hpp 中有一个变量: namespace TRIALS { static thread_local int a = -1; } 我在 ema.hpp/ema.cpp
我认为如果函数不修改非本地数据,它们就是线程安全的。 根据这个answer,我的假设是正确的.但是,最近我遇到了这段代码, int intRand(const int & min, const int
我在销毁 thread_local 静态对象时遇到问题。 #include #include struct UsesLoc { UsesLoc() { loc.counte
我认为如果函数不修改非本地数据,它们就是线程安全的。 根据这个answer,我的假设是正确的.但是,最近我遇到了这段代码, int intRand(const int & min, const int
在我的代码中使用 thread_local 之前,我想更好地理解它。 比方说,我声明 thread_local myclass value; 这将为每个使用 myclass 的线程创建 value 的
C.h: #include class C { public: explicit C(int id) { std::cout<<"Initialized "<
我知道这是一个非常基本的问题,但我找不到简单的答案。 我正在编写一个程序,其中我需要一些变量是 thread_local。根据我的理解,这意味着这些变量“像全局变量”,但每个线程都有自己的拷贝。 我以
在问题Using QSqlQuery from multiple threads结果是线程存储解决了这个问题。 我制作了一个简单的演示代码,以绝对清楚 C++11 thread_local 说明符。下
我有以下 thread_local 单例代码: struct Singl { int& ref; Singl(int& r) : ref(r) {} ~Singl() {} void
我想了解 thread_local 限定符究竟是如何工作的,以及实际变量存储在哪里?这是在 C++ 上。 假设我有一个包含多个成员变量的类。该类的对象在堆上实例化,该对象在 2 个线程之间共享。使用适
#include #include #include using namespace std; struct A { ~A() { cout << "~A()"
现在 C++ 正在添加 thread_local 存储作为语言功能,我想知道一些事情: thead_local 的成本可能是多少? 在内存中? 用于读写操作? 与此相关:操作系统通常如何实现这一点?似
考虑以下示例(为简单起见,省略了 cout 上的锁守卫)。 #include #include #include using namespace std; struct C { C() {
我正在努力解决 C++ Crash Course我遇到了以下代码 list : #include struct Tracer { Tracer(const char* name) :
我想在我的类中进行一些线程注册,因此我决定添加对 thread_local 功能的检查: #include #include class Foo { public: Foo() {
下面的代码应该初始化一个静态线程局部和静态结构。 #include struct Tracer { public: Tracer(const char *new_name) : name{new
我有一个我想构造的类成员,它应该是访问它的每个线程的本地成员。虽然构造函数需要一些参数,所以我不能依赖静态零初始化。 class ThreadMem{ public: ThreadMem(ui
我试图声明 thread_local 指针变量,然后在一个线程中指向一个新对象。 thread_local static A* s_a = nullptr; 好像线程销毁的时候新对象的内存没有释放。我
我是一名优秀的程序员,十分优秀!