Created
July 26, 2018 15:02
-
-
Save wush978/36c4e5d8324dd14040eecb4b1dd1c631 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
#include <thread> | |
using namespace Rcpp; | |
//[[Rcpp::export]] | |
void testInterrupt(const int jsize, bool verbose, const int out_index = 0) { | |
std::ostream* pout; | |
if (out_index == 0) pout = &Rcout; | |
else if (out_index == 1) pout = &std::cout; | |
else throw std::invalid_argument("out"); | |
std::ostream& out(*pout); | |
bool is_interrupted = false; | |
{ | |
for(int i = 0;i < 10;i++) { | |
for(int j = 0;j < jsize;j++) { | |
{ | |
if (verbose) { | |
out << "."; | |
out.flush(); | |
} | |
} | |
std::this_thread::sleep_for(std::chrono::milliseconds(5000 / jsize)); | |
} | |
{ | |
out << std::endl; | |
try { | |
checkUserInterrupt(); | |
} catch (...) { | |
std::cerr << "setting is_interrupted" << std::endl; | |
is_interrupted = true; | |
} | |
} | |
if (is_interrupted) { | |
break; | |
} | |
} | |
} | |
if (is_interrupted) { | |
std::cerr << "Interrupted!" << std::endl; | |
throw Rcpp::internal::InterruptedException(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment