linux运行pthread无反应,c – gcc 4.7 on linux pthreads – 非常重要的thread_local解决方法使用__thread(无提升)...

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

class X { ... }

void f()

{

thread_local X x = ...;

...

}

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

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

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

这是我到目前为止:

#include

#include

using namespace std;

class X

{

public:

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

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

};

typedef aligned_storage::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();

}

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

更新:

我认为pthread_cleanup_push可能就是我想要的:

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

更新2:

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值