I am trying to compile this on msvc 12.0, the code in the book uses the keyword thread_local but it seems msvc 12.0 does not support this? Instead of using thread_local I tried __declspec(thread) but I get the following compiler error:
Error 1 error C2483: 'this_thread_interrupt_flag' : object with constructor or destructor cannot be declared 'thread'
Here is my code:
#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;
};
Is there any workarounds for this in msvc 12.0?