0
  • こんにちは、ロボットのプログラミングに 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;
4

1 に答える 1

1

それは gcc 固有のThread-Local Storageです。スレッド ローカル データをサポートする標準的な方法として、 C11 が追加され_Thread_local、C++11 が追加されることに注意してください。thread_local

于 2012-12-28T18:45:43.963 に答える