<div dir="ltr">Yes, the only reason to use Rcpp::stop is because you want a C++ exception translated safely into an Rf_error at the SEXP call level. If you are trying to signal an error from a background thread you would want to use a regular C++ exception rather than Rcpp::stop.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Feb 8, 2020 at 1:56 PM Iñaki Ucar <<a href="mailto:iucar@fedoraproject.org">iucar@fedoraproject.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Fri, 7 Feb 2020 at 14:24, Joshua N Pritikin <<a href="mailto:jpritikin@pobox.com" target="_blank">jpritikin@pobox.com</a>> wrote:<br>
><br>
> On Thu, Feb 06, 2020 at 08:40:02PM -0600, Dirk Eddelbuettel wrote:<br>
> > On 6 February 2020 at 20:47, Joshua N Pritikin wrote:<br>
> > | The Rcpp::exception constructor does,<br>
> > |<br>
> > |   rcpp_set_stack_trace(Shield<SEXP>(stack_trace()))<br>
> > |<br>
> > | This can corrupt R if called within an OpenMP block.<br>
> ><br>
> > ... here. In general, we can _never_ call back into R for anything,<br>
> > exceptions or other things.<br>
> ><br>
> > The RcppParallel package documentation is quite good and clear about<br>
> > this; it even has extra data types RMatrix and RVector to stay away<br>
> > from R's memory (which Rcpp is close to for performance and zero<br>
> > copy reasons). The Writing R Extensions manual also as a little, but<br>
> > maybe less clearly.  In short, there is simply "so much going with<br>
> > R" that it stands little chance of every being threadsafe.<br>
> ><br>
> > Which means that your OpenMP (or pthreads or TBB or ..) code has to<br>
> > stay away from R.<br>
><br>
> Yeah, so I replaced Rcpp::stop with,<br>
><br>
> template <typename... Args><br>
> inline void NORET mxThrow(const char* fmt, Args&&... args) {<br>
>     throw std::runtime_error( tfm::format(fmt, std::forward<Args>(args)... ).c_str() );<br>
> }<br>
><br>
> And now things work great. But why does Rcpp::stop need to get the<br>
> stack_trace? R's stack trace isn't going to change until the control<br>
> flow returns back to R. So why can't you just set a flag to indicate<br>
> that "some C++ exception happened" and grab the stack_trace<br>
> immediately before returning control to R?<br>
<br>
Isn't Rcpp::stop's entire purpose to return control to R immediately?<br>
<br>
Iñaki<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div>