我的问题是来自“static __thread Thread * threadData;”的行和“__thread AssertFramework :: Thread * AssertFramework :: threadData = 0;”,你能猜出“__thread”是什么意思吗?它是一个类型吗?一个特殊功能的名称?指向一个线程的指针???
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <cstring>
...
class AssertFramework
{
public:
struct Line
{
char file[128];
int line;
char message[128];
};
struct Track
{
Line line[16];
int currentLine;
bool active;
};
struct Thread
{
char name[32];
Track track[2];
};
struct Data
{
Thread thread[2];
int currentThread;
};
static pthread_mutex_t mutex;
static __thread Thread* threadData;
int fd;
Data* data;
AssertFramework() : fd(-1), data((Data*)MAP_FAILED) {}
~AssertFramework()
{
if(data != MAP_FAILED)
munmap(data, sizeof(Data));
if(fd != -1)
close(fd);
}
bool init(bool reset)
{
if(data != MAP_FAILED)
return true;
fd = shm_open("/bhuman_assert", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if(fd == -1)
return false;
if(ftruncate(fd, sizeof(Data)) == -1 ||
(data = (Data*)mmap(NULL, sizeof(Data), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
{
close(fd);
fd = -1;
return false;
}
if(reset)
memset(data, 0, sizeof(Data));
return true;
}
} assertFramework;
pthread_mutex_t AssertFramework::mutex = PTHREAD_MUTEX_INITIALIZER;
__thread AssertFramework::Thread* AssertFramework::threadData = 0;
答案 0 :(得分:1)
那是特定于gcc的Thread-Local Storage。请注意,C11添加_Thread_local
,C ++ 11添加thread_local
作为支持线程本地数据的标准方法。