user-0 提问: C语言多线程 openmp 

回复:
user: user-1
content: 我用线程的话一般是包装成类，每个线程就是一个类对象，很方便保存各种参数

user: user-2
content: Thread Local Storage Microsoft Specific —>  Thread Local Storage (TLS) is the mechanism by which each thread in a given 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, seeProcesses and Threads in the Microsoft Win32® Software Development Kit documentation.  The Microsoft C language includes the extended storage-class attribute, thread, which is used with the __declspec keyword to declare a thread local variable. For example, the following code declares an integer thread local variable and initializes it with a value:  __declspec( thread ) int tls_i = 1;  These guidelines must be observed when you are declaring statically bound thread local variables:   The use of __declspec(thread) may interfere withdelay loading of DLL imports.   You can apply the thread attribute only to data declarations and definitions. It cannot be used on function declarations or definitions. For example, the following code generates a compiler error: #define Thread    __declspec( thread ) Thread void func();        /* Error */  You can specify the thread attribute only on data items with static storage duration. This includes global data (both static and extern) and local static data. You cannot declare automatic data with the thread attribute. For example, the following code generates compiler errors: #define Thread    __declspec( thread ) void func1() {     Thread int tls_i;            /* Error */ }  int func2( Thread int tls_i )    /* Error */ { return tls_i; }  You must use the thread attribute for the declaration and the definition of thread local data, regardless of whether the declaration and definition occur in the same file or separate files. For example, the following code generates an error: #define Thread    __declspec( thread ) extern int tls_i;     /* This generates an error, because the   */ int Thread tls_i;     /* declaration and the definition differ. */  You cannot use the thread attribute as a type modifier. For example, the following code generates a compiler error: char *ch __declspec( thread );        /* Error */  The address of a thread local variable is not considered constant, and any expression involving such an address is not considered a constant expression. This means that you cannot use the address of a thread local variable as an initializer for a pointer. For example, the compiler flags the following code as an error: #define Thread    __declspec( thread ) Thread int tls_i; int *p = &tls_i;        /* Error */  C permits initialization of a variable with an expression involving a reference to itself, but only for objects of nonstatic extent. For example: #define Thread    __declspec( thread ) Thread int tls_i = tls_i;             /* Error */ int j = j;                            /* Error */ Thread int tls_i = sizeof( tls_i )    /* Okay  */  Note that a sizeof expression that includes the variable being initialized does not constitute a reference to itself and is allowed.  The use of __declspec(thread) may interfere withdelay loading of DLL imports.  For more information about using the thread attribute, seeMultithreading Topics in Visual C++ Programmer’s Guide.  END Microsoft Specific

user: user-2
content: 引用 7 楼 paschen 的回复:引用 6 楼 hezhe1008 的回复:引用 3 楼 xu6832281 的回复:[quote=引用 2楼呔妖怪来嘛 的回复:]你的意思是多个线程访问同一块内存？？如果是这样，访问会有冲突，需要增加锁，很难并行计算我的意思是怎么让各个线程都有一份独立的啥叫每个线程都有一份独立的，又全局，又独立的~~。你多整几个全局变量不就行了？？  使用线程局部存储(TLS)，具体用法可参看MSDN[/quote]   Thread Local Storage (TLS) Home |  Overview |  How Do I |  Sample  Thread Local Storage (TLS) is the method by which each thread in a given multithreaded process may allocate locations in which to store thread-specific data. Dynamically bound (run-time) thread-specific data is supported by way of the TLS API (TlsAlloc, TlsGetValue, TlsSetValue, TlsFree). Win32 and the Visual C++ compiler now support statically bound (load-time) per-thread data in addition to the existing API implementation.  API Implementation for TLS Thread Local Storage is implemented through the Win32 API layer as well as the compiler. For details, see the Win32 API documentation for TlsAlloc, TlsGetValue, TlsSetValue, and TlsFree.  The Visual C++ compiler includes a keyword to make thread local storage operations more automatic, rather than through the API layer. This syntax is described in the next section, Compiler Implementation for TLS.  Compiler Implementation for TLS To support TLS, a new attribute, thread, has been added to the C and C++ languages and is supported by the Visual C++ compiler. This attribute is an extended storage class modifier, as described in the previous section. Use the __declspec keyword to declare a thread variable. For example, the following code declares an integer thread local variable and initializes it with a value:  __declspec( thread ) int tls_i = 1;  See Also   Rules and Limitations for TLS

user: user-3
content: http://blog.csdn.net/gengshenghong/article/details/6985431

user: user-3
content: threadprivate指定全局变量被OpenMP所有的线程各自产生一个私有的拷贝，即各个线程都有自己私有的全局变量。threadprivate并不是针对某一个并行区域，而是整个于整个程序，所以其拷贝的副本变量也是全局的，即在不同的并行区域之间的同一个线程也是共享的。threadprivate只能用于全局变量或静态变量

user: user-2
content: 《Windows核心编程》

user: user-0
content: 我的意思是：threadprivate声明的指针变量在各个线程都有一份独立拷贝，互不影响。但这前提是在同一个文件中，如果跨文件的全局变量，这种机制就出错了

user: user-0
content: 引用 2楼呔妖怪来嘛 的回复:你的意思是多个线程访问同一块内存？？ 如果是这样，访问会有冲突，需要增加锁，很难并行计算我的意思是怎么让各个线程都有一份独立的

user: user-4
content: 你的意思是多个线程访问同一块内存？？ 如果是这样，访问会有冲突，需要增加锁，很难并行计算

user: user-5

user: user-3
content: 引用 6 楼 hezhe1008 的回复:引用 3 楼 xu6832281 的回复:引用 2楼呔妖怪来嘛 的回复:你的意思是多个线程访问同一块内存？？如果是这样，访问会有冲突，需要增加锁，很难并行计算我的意思是怎么让各个线程都有一份独立的啥叫每个线程都有一份独立的，又全局，又独立的~~。你多整几个全局变量不就行了？？  使用线程局部存储(TLS)，具体用法可参看MSDN

user: user-4
content: 引用 3 楼 xu6832281 的回复:[quote=引用 2楼呔妖怪来嘛 的回复:]你的意思是多个线程访问同一块内存？？ 如果是这样，访问会有冲突，需要增加锁，很难并行计算我的意思是怎么让各个线程都有一份独立的[/quote]  啥叫每个线程都有一份独立的，又全局，又独立的~~。你多整几个全局变量不就行了？？

user: user-2
content: 访问改数组变量时→访问该数组变量时

user: user-2
content: 线程函数中声明的占内存比较小的变量本来就在各个线程的堆栈中。 线程函数中声明的占内存比较大（比如超过1MB）的变量应该在前面加static，将其存储位置挪到全局变量区，或者使用new，将其挪到堆存储区；但这样一来，会导致多线程读写冲突，此时一个解决办法就是将变量数组化，每个线程访问改数组变量时，都使用比如varArray[thread_id%MAX_THREADS]的办法避免访问冲突。