Skip to content

Instantly share code, notes, and snippets.

@wush978
Created July 26, 2018 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wush978/36c4e5d8324dd14040eecb4b1dd1c631 to your computer and use it in GitHub Desktop.
Save wush978/36c4e5d8324dd14040eecb4b1dd1c631 to your computer and use it in GitHub Desktop.
#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