linux pthreads上的gcc 4.7 - 使用__thread的非常重要的thread_local解决方法(无提升)

And*_*zos 8 c++ linux multithreading pthreads c++11

在C++ 11中,您可以使用thread_local存储创建一个非平凡的对象:

class X { ... }

void f()
{
    thread_local X x = ...;
    ...
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这个功能尚未在gcc中实现(截至4.7).

gcc允许你拥有线程局部变量,但只允许使用普通类型.

我正在寻找一个解决方法:

这是我到目前为止:

#include <iostream>
#include <type_traits>

using namespace std;

class X
{
public:
    X() { cout << "X::X()" << endl; };
    ~X() { cout << "X::~X()" << endl; }
};

typedef aligned_storage<sizeof(X), alignment_of<X>::value>::type XStorage;

inline void placement_delete_x(X* p) { p->~X(); }

void f()
{
        static __thread bool x_allocated = false;
        static __thread XStorage x_storage;

        if (!x_allocated)
        {
                new (&x_storage) X;
                x_allocated = true;

                // TODO: add thread cleanup that
                //     calls placement_delete_x(&x_storage)
        }

        X& x = *((X*) &x_storage);
}

int main()
{
        f();
}
Run Code Online (Sandbox Code Playgroud)

我需要帮助的是在退出当前线程时调用placement_delete_x(&x_storage).我可以使用pthreads和/或linux中的机制来做到这一点吗?我需要在某种pthread清理堆栈中添加一个函数指针和一个参数?

更新:

我想pthread_cleanup_push可能是我想要的:

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_cleanup_push.3.html

这会在正确的情况下调用清理处理程序吗?

更新2:

看起来boost::thread_specific_ptr最终pthread_key_create使用destructor参数调用,而不是pthread_cleanup_push- 调用它的tls清理函数:

http://pubs.opengroup.org/onlinepubs/009696799/functions/pthread_key_create.html

目前还不清楚这两种方法之间的区别是什么,如果有的话.?

And*_*zos 3

正如迈克所说,pthread_cleanup_push这是不合适的。正确的方法是使用pthread_key_create.

我已经实现了一个小型演示程序来展示如何做到这一点。我们实现了一个宏thread_local,您可以这样使用:

真正的 C++11 功能将是:

void f()
{
    thread_local X x(1,2,3);
    ...
}
Run Code Online (Sandbox Code Playgroud)

有了这个,就是:

void f()
{
    thread_local (X, x, 1, 2, 3);
    ...
}
Run Code Online (Sandbox Code Playgroud)

它与 boost::thread_specifc_ptr 的区别在于动态内存分配为零。一切都按期限存储__thread。它的重量也明显更轻,但它是 gcc/linux 特定的。

概述:

  1. 我们曾经std::aligned_storage为变量创建__thread持续时间空间
  2. 在给定线程的第一个条目中,我们使用placement new 来构造存储中的变量
  3. 我们还__thread为放置删除调用分配一个链表条目
  4. 我们用来pthread_setspecific跟踪每个线程列表头
  5. 当线程退出时,传递给该函数的函数pthread_key_create会遍历调用放置删除的列表。

...

#include <iostream>
#include <thread>

using namespace std;

static pthread_key_t key;
static pthread_once_t once_control = PTHREAD_ONCE_INIT;

struct destructor_list
{
    void (*destructor)(void*);
    void* param;
    destructor_list* next;
};

static void execute_destructor_list(void* v)
{
    for (destructor_list* p = (destructor_list*) v; p != 0; p = p->next)
        p->destructor(p->param);
}

static void create_key()
{
    pthread_key_create(&key, execute_destructor_list);
}

void add_destructor(destructor_list* p)
{
    pthread_once(&once_control, create_key);

    p->next = (destructor_list*) pthread_getspecific(key);
    pthread_setspecific(key, p);
}

template<class T> static void placement_delete(void* t) { ((T*)t)->~T(); }

#define thread_local(T, t, ...)                         \
T& t = *((T*)                                           \
({                                                      \
    typedef typename aligned_storage<sizeof(T),         \
        alignment_of<T>::value>::type Storage;          \
    static __thread bool allocated = false;             \
    static __thread Storage storage;                    \
    static __thread destructor_list dlist;              \
                                                        \
    if (!allocated)                                     \
    {                                                   \
        new (&storage) T(__VA_ARGS__);                  \
        allocated = true;                               \
        dlist.destructor = placement_delete<T>;         \
        dlist.param = &storage;                         \
        add_destructor(&dlist);                         \
    }                                                   \
                                                        \
    &storage;                                           \
}));

class X
{
public:
    int i;

    X(int i_in) { i = i_in; cout << "X::X()" << endl; };

    void f() { cout << "X::f()" << endl; }

    ~X() { cout << "X::~X() i = " << i << endl; }
};

void g()
{
    thread_local(X, x, 1234);
    x.f();
}

int main()
{
    thread t(g);
    t.join();
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 您需要为每个 pthread_* 调用添加错误检查。我只是将其删除以进行说明。
  2. 它使用的__thread是 GNU 扩展
  3. 它使用表达式语句将辅助 __thread 变量名称保留在父范围之外。这也是一个 GNU 扩展。