add original source
This commit is contained in:
31
source/listing_9.9.cpp
Normal file
31
source/listing_9.9.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user