具有多线程的静态变量

Syn*_*ror 0 c static multithreading

我有一个多线程的C代码,我想让一个全局变量成为线程私有的.那就是每个线程都拥有它自己的副本......这样做的最佳方式是什么?

pic*_*c11 5

你想要的是TLS.TLS的声明与任何其他全局(静态)变量一样,但语法依赖于实现.例如:

// Visual C/C++ and Intel C/C++ on Windows
__declspec(thread) int number;

// GCC and Intel C/C++ on Linux
__thread int number;
Run Code Online (Sandbox Code Playgroud)

Boost和TBB有自己的便携式TLS,但它是C++,而不是C.