C++ 用c+编译问题+;运行中的并发性清单9.9(msvc 12.0)

C++ 用c+编译问题+;运行中的并发性清单9.9(msvc 12.0),c++,c++11,stdthread,thread-local-storage,C++,C++11,Stdthread,Thread Local Storage,我试图在msvc 12.0上编译这个,书中的代码使用关键字thread_ulocal,但是它似乎不支持msvc 12.0?我没有使用thread\u local,而是尝试了declspec(thread),但出现了以下编译器错误: Error 1 error C2483: 'this_thread_interrupt_flag' : object with constructor or destructor cannot be declared 'thread' 这是我的密码:

我试图在msvc 12.0上编译这个,书中的代码使用关键字thread_ulocal,但是它似乎不支持msvc 12.0?我没有使用thread\u local,而是尝试了declspec(thread),但出现了以下编译器错误:

Error   1   error C2483: 'this_thread_interrupt_flag' : object with constructor or destructor cannot be declared 'thread'   
这是我的密码:

#include <thread>
#include <atomic>
#include <future>

class interrupt_flag
{
public:
    void set()
    {
        flag.store(true, std::memory_order_relaxed);
    }

    bool is_set() const
    {
        return flag.load(std::memory_order_relaxed);
    }

private:
    std::atomic<bool> flag;

};

__declspec(thread) interrupt_flag this_thread_interrupt_flag;

class interruptible_thread
{
public:

    template<typename FunctionType>
    interruptible_thread(FunctionType f)
    {
        std::promise<interrupt_flag*> p;
        internal_thread = std::thread([f, &p] {
                p.set_value(&this_thread_interrupt_flag);
                f();
            });
        flag = p.get_future().get();
    }

    void interrupt()
    {
        if (flag)
        {
            flag->set();
        }
    }

private:
    std::thread internal_thread;
    interrupt_flag* flag;
#包括
#包括
#包括
类中断标志
{
公众:
空集()
{
flag.store(true,std::memory\u order\u released);
}
布尔是集合()常量
{
返回标志。加载(标准::内存\u顺序\u松弛);
}
私人:
原子标志;
};
__declspec(线程)中断\u标志此\u线程\u中断\u标志;
类可中断线程
{
公众:
模板
可中断线程(函数类型f)
{
std::promise p;
内部螺纹=标准::螺纹([f,&p]{
p、 设置\u值(&此\u线程\u中断\u标志);
f();
});
flag=p.get_future().get();
}
无效中断()
{
国际单项体育联合会(旗)
{
flag->set();
}
}
私人:
标准:螺纹内螺纹;
中断标志*标志;
})


在msvc 12.0中是否有任何解决方法?

正如您所发现的,msvc还不支持
thread\u local
(也不支持线程安全的静态局部变量初始化),但在下一个版本(14)中会支持。不幸的是,在此之前,您必须使用互斥锁或只是避免这种限制。