[Rd] Interrupting C++ code execution

schattenpflanze at arcor.de schattenpflanze at arcor.de
Mon Apr 25 11:22:15 CEST 2011


I am writing an R interface for one of my C++ programs. The computations 
in C++ are very time consuming (several hours), so the user needs to be 
able to interrupt them. Currently, the only way I found to do so is 
calling R_CheckUserInterrupt() frequently. Unfortunately, there are 
several problems with that:

1. Calling R_CheckUserInterrupt() interrupts immediately, so I have no 
possibility to exit my code gracefully. In particular, I suppose that 
objects created on the heap (e.g., STL containers) are not destructed 

2. Calling R_CheckUserInterrupt() within a parallel OpenMP loop causes 
memory corruptions. Even if I do so within a critical section, it 
usually results in segfaults, crashes, or invalid variable contents 
afterwards. I suppose this is due to the threads not being destroyed 
properly. Since most of the time critical computations are done in 
parallel, this means I can hardly interrupt anything.

Having a function similar to R_CheckUserInterrupt() but returning a 
boolean variable (has an interrupt occurred or not?) would solve these 
problems. Is there a way to find out about user interrupt requests (the 
user pressing ctrl+c or maybe a different set of keys) without 
interrupting immediately?

I would appreciate your advice on this topic.

Best regards,

More information about the R-devel mailing list