- 嗨,我正在使用 C 库对机器人进行编程。在阅读代码时,我遇到了“_线程”这个术语,我不知道它是什么意思。我试图搜索项目以查看“_thread”上是否有任何定义,但这对我来说没有意义。我猜下面的代码可能与我的问题有关。
我的问题是从“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;