相关疑难解决方法(0)

C++异常是否足以实现线程本地存储?

我正在评论一个线程本地存储很好的答案,并回忆起关于异常的另一个信息性讨论

throw块中执行环境的唯一特殊之处在于rethrow引用了异常对象.

将两个和两个放在一起,不会在其主函数的函数catch块中执行整个线程,并将其与线程本地存储一起使用?

虽然很慢,但似乎工作正常.这是小说还是很有特色?还有另一种解决问题的方法吗?我最初的前提是否正确?get_thread您的平台会产生什么样的开销?优化的潜力是什么?

#include <iostream>
#include <pthread.h>
using namespace std;

struct thlocal {
    string name;
    thlocal( string const &n ) : name(n) {}
};

struct thread_exception_base {
    thlocal &th;
    thread_exception_base( thlocal &in_th ) : th( in_th ) {}
    thread_exception_base( thread_exception_base const &in ) : th( in.th ) {}
};

thlocal &get_thread() throw() {
    try {
        throw;
    } catch( thread_exception_base &local ) {
        return local.th;
    }
}

void print_thread() {
    cerr << get_thread().name …
Run Code Online (Sandbox Code Playgroud)

c++ exception thread-local

26
推荐指数
1
解决办法
1203
查看次数

标签 统计

c++ ×1

exception ×1

thread-local ×1