嗨,我正在使用C库对机器人进行编程。阅读时
代码中,我遇到了术语“ _thread”,我不知道该怎么办
是不是意思我试图搜索该项目以查看是否
“ _thread”上有任何定义,但没有意义
对我来说。我猜下面的代码可能与我的问题有关。
我的问题是从“静态__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;
最佳答案
那是gcc特定的Thread-Local Storage。请注意,C11添加_Thread_local
,而C ++ 11添加thread_local
作为支持线程本地数据的标准方法。
关于c - Ubuntu上的“__thread”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14073168/