You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The **thread** extended storage-class modifier is used to declare a thread local variable. For the portable equivalent in C++11, use the [thread_local](../cpp/storage-classes-cpp.md#thread_local) storage class specifier.
24
-
25
-
## Syntax
26
-
27
-
```
28
-
29
-
__declspec( thread ) declarator
30
-
```
31
-
32
-
## Remarks
33
-
Thread Local Storage (TLS) is the mechanism by which each thread in a multithreaded process allocates storage for thread-specific data. In standard multithreaded programs, data is shared among all threads of a given process, whereas thread local storage is the mechanism for allocating per-thread data. For a complete discussion of threads, see [Multithreading](../parallel/multithreading-support-for-older-code-visual-cpp.md).
34
-
35
-
Declarations of thread local variables must use [extended attribute syntax](../cpp/declspec.md) and the `__declspec` keyword with the **thread** keyword. For example, the following code declares an integer thread local variable and initializes it with a value:
36
-
37
-
```
23
+
The **thread** extended storage-class modifier is used to declare a thread local variable. For the portable equivalent in C++11 and later, use the [thread_local](../cpp/storage-classes-cpp.md#thread_local) storage class specifier.
24
+
25
+
## Syntax
26
+
27
+
```
28
+
__declspec( thread ) declarator
29
+
```
30
+
31
+
## Remarks
32
+
33
+
Thread Local Storage (TLS) is the mechanism by which each thread in a multithreaded process allocates storage for thread-specific data. In standard multithreaded programs, data is shared among all threads of a given process, whereas thread local storage is the mechanism for allocating per-thread data. For a complete discussion of threads, see [Multithreading](../parallel/multithreading-support-for-older-code-visual-cpp.md).
34
+
35
+
Declarations of thread local variables must use [extended attribute syntax](../cpp/declspec.md) and the `__declspec` keyword with the **thread** keyword. For example, the following code declares an integer thread local variable and initializes it with a value:
36
+
37
+
```cpp
38
38
__declspec( thread ) int tls_i = 1;
39
-
```
40
-
41
-
You must observe these guidelines when declaring thread local objects and variables:
42
-
43
-
- You can apply the **thread** attribute only to class and data declarations and definitions; **thread** cannot be used on function declarations or definitions.
44
-
45
-
- The use of the **thread** attribute may interfere with [delay loading](../build/reference/linker-support-for-delay-loaded-dlls.md) of DLL imports**.**
46
-
47
-
- On XP systems, `thread` may not function correctly if a DLL uses __declspec(thread) data and it is loaded dynamically via LoadLibrary.
48
-
49
-
- You can specify the **thread** attribute only on data items with static storage duration. This includes global data objects (both **static** and `extern`), local static objects, and static data members of classes. You cannot declare automatic data objects with the **thread** attribute.
50
-
51
-
- You must use the **thread** attribute for the declaration and the definition of a thread local object, whether the declaration and definition occur in the same file or separate files.
52
-
53
-
- You cannot use the **thread** attribute as a type modifier.
54
-
55
-
- Because the declaration of objects that use the **thread** attribute is permitted, these two examples are semantically equivalent:
- Standard C permits initialization of an object or variable with an expression involving a reference to itself, but only for objects of nonstatic extent. Although C++ normally permits such dynamic initialization of an object with an expression involving a reference to itself, this type of initialization is not permitted with thread local objects. For example:
73
-
74
-
```
75
-
// declspec_thread_3.cpp
76
-
// compile with: /LD
77
-
#define Thread __declspec( thread )
78
-
int j = j; // Okay in C++; C error
79
-
Thread int tls_i = sizeof( tls_i ); // Okay in C and C++
80
-
```
81
-
82
-
Note that a `sizeof` expression that includes the object being initialized does not constitute a reference to itself and is allowed in C and C++.
83
-
84
-
**END Microsoft Specific**
85
-
86
-
## See Also
87
-
[__declspec](../cpp/declspec.md)
88
-
[Keywords](../cpp/keywords-cpp.md)
89
-
[Thread Local Storage (TLS)](../parallel/thread-local-storage-tls.md)
39
+
```
40
+
41
+
You must observe these guidelines when declaring thread local objects and variables:
42
+
43
+
- You can apply the **thread** attribute only to class and data declarations and definitions; **thread** cannot be used on function declarations or definitions.
44
+
45
+
- The use of the **thread** attribute may interfere with [delay loading](../build/reference/linker-support-for-delay-loaded-dlls.md) of DLL imports.
46
+
47
+
- On XP systems, **thread** may not function correctly if a DLL uses __declspec(thread) data and it is loaded dynamically via LoadLibrary.
48
+
49
+
- You can specify the **thread** attribute only on data items with static storage duration. This includes global data objects (both **static** and **extern**), local static objects, and static data members of classes. You cannot declare automatic data objects with the **thread** attribute.
50
+
51
+
- You must use the **thread** attribute for the declaration and the definition of a thread local object, whether the declaration and definition occur in the same file or separate files.
52
+
53
+
- You cannot use the **thread** attribute as a type modifier.
54
+
55
+
- Because the declaration of objects that use the **thread** attribute is permitted, these two examples are semantically equivalent:
- Standard C permits initialization of an object or variable with an expression involving a reference to itself, but only for objects of nonstatic extent. Although C++ normally permits such dynamic initialization of an object with an expression involving a reference to itself, this type of initialization is not permitted with thread local objects. For example:
73
+
74
+
```cpp
75
+
// declspec_thread_3.cpp
76
+
// compile with: /LD
77
+
#define Thread __declspec( thread )
78
+
int j = j; // Okay in C++; C error
79
+
Thread int tls_i = sizeof( tls_i ); // Okay in C and C++
80
+
```
81
+
82
+
Note that a **sizeof** expression that includes the object being initialized does not constitute a reference to itself and is allowed in C and C++.
83
+
84
+
**END Microsoft Specific**
85
+
86
+
## See Also
87
+
88
+
[__declspec](../cpp/declspec.md)
89
+
[Keywords](../cpp/keywords-cpp.md)
90
+
[Thread Local Storage (TLS)](../parallel/thread-local-storage-tls.md)
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-1/compiler-error-c2482.md
+19-16Lines changed: 19 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Compiler Error C2482 | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "09/15/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -35,18 +35,21 @@ translation.priority.ht:
35
35
- "zh-tw"
36
36
---
37
37
# Compiler Error C2482
38
-
'identifier' : dynamic initialization of 'thread' data not allowed
39
-
40
-
Variables declared with the `thread` attribute cannot be initialized with an expression that requires run-time evaluation. A static expression is required to initialize `thread` data.
41
-
42
-
The following sample generates C2482:
43
-
44
-
```
45
-
// C2482.cpp
46
-
// compile with: /c
47
-
#define Thread __declspec( thread )
48
-
Thread int tls_i = tls_i; // C2482
49
-
50
-
int j = j; // OK in C++; C error
51
-
Thread int tls_i = sizeof( tls_i ); // Okay in C and C++
52
-
```
38
+
39
+
>'*identifier*' : dynamic initialization of 'thread' data not allowed
40
+
41
+
This error message is obsolete in Visual Studio 2015 and later versions. In previous versions, variables declared by using the `thread` attribute cannot be initialized with an expression that requires run-time evaluation. A static expression is required to initialize `thread` data.
42
+
43
+
## Example
44
+
45
+
The following sample generates C2482 in Visual Studio 2013 and earlier:
46
+
47
+
```cpp
48
+
// C2482.cpp
49
+
// compile with: /c
50
+
#defineThread__declspec( thread )
51
+
Thread int tls_i = tls_i; // C2482
52
+
53
+
int j = j; // OK in C++; C error
54
+
Thread int tls_i = sizeof( tls_i ); // Okay in C and C++
Copy file name to clipboardExpand all lines: docs/error-messages/compiler-errors-1/compiler-error-c2483.md
+23-20Lines changed: 23 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Compiler Error C2483 | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "09/15/2017"
5
5
ms.reviewer: ""
6
6
ms.suite: ""
7
7
ms.technology:
@@ -35,23 +35,26 @@ translation.priority.ht:
35
35
- "zh-tw"
36
36
---
37
37
# Compiler Error C2483
38
-
'identifier' : object with constructor or destructor cannot be declared 'thread'
39
-
40
-
Variables declared with the `thread` attribute cannot be initialized with a constructor or other expression that requires run-time evaluation. A static expression is required to initialize `thread` data.
41
-
42
-
## Example
43
-
The following sample generates C2483.
44
-
45
-
```
46
-
// C2483.cpp
47
-
// compile with: /c
48
-
__declspec(thread) struct A {
49
-
A(){}
50
-
~A(){}
38
+
39
+
>'*identifier*' : object with constructor or destructor cannot be declared 'thread'
40
+
41
+
This error message is obsolete in Visual Studio 2015 and later versions. In previous versions, variables declared with the `thread` attribute cannot be initialized with a constructor or other expression that requires run-time evaluation. A static expression is required to initialize `thread` data.
42
+
43
+
## Example
44
+
45
+
The following sample generates C2483 in Visual Studio 2013 and earlier versions.
0 commit comments