Windows下Visual C/C++ __declspec(thread)与Linux下C的__thread

xingyun86 2019-8-12 1510

Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread.

This is sometimes needed because normally all threads in a process share the same address space, which is sometimes undesirable.

C++0x introduces the thread_local keyword. Aside that, various C++ compiler implementations provide specific ways to declare thread-local variables:
Sun Studio C/C++, IBM XL C/C++, GNU C and Intel C/C++ (Linux systems) use the syntax:
    __thread int number;
Visual C++, Intel C/C++ (Windows systems), Borland C++ Builder and Digital Mars C++ use the syntax:
    __declspec(thread) int number;
Borland C++ Builder also supports the syntax:
    int __thread number;

So, whilst __thread does exist in practice and on some systems, thread_local is the new, official, C++0x keyword that does the same thing.

Prefer it to non-standard __thread whenever you have access to C++0x.


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回