add original source

This commit is contained in:
Bo Wang
2014-04-22 21:54:16 -04:00
parent f7a05be5e1
commit 6e45d14431
128 changed files with 5726 additions and 0 deletions

31
source/listing_9.9.cpp Normal file
View File

@@ -0,0 +1,31 @@
class interrupt_flag
{
public:
void set();
bool is_set() const;
};
thread_local interrupt_flag this_thread_interrupt_flag;
class interruptible_thread
{
std::thread internal_thread;
interrupt_flag* flag;
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();
}
}
};