title: Armadillo's print() method and cout are inconsistent in order when called from Rcpp
body: <p>Lately, I've been using <code>RcppArmadillo</code>, but I've noticed some inconsistencies in the print order of certain objects. Particularly, when using <code>cout</code> and <code>print()</code>. Sometimes, <code>print()</code> will be printed first, then <code>cout</code>; and other times it's the other way around. </p>
  
  <p>I do not understand exactly why this is happening. I suppose <code>cout</code> and <code>print()</code> are called asynchronously and hence the difference in order, but why is this happening? And how can it be prevented?</p>
  
  <h3>Example</h3>
  
  <p>If I have the following <code>test_order.cpp</code> file</p>
  
  <pre><code>#include &lt;RcppArmadillo.h&gt;
  
  // [[Rcpp::depends(RcppArmadillo)]]
  
  using namespace Rcpp;
  using namespace arma;
  
  
  // [[Rcpp::export]]
  int test(int n){
  
    cout &lt;&lt; "Print 1\n";
    arma::mat X(n, n);
    cout &lt;&lt; "Print 2\n";
    X.print();
    cout &lt;&lt; "Print 3\n";
  
    return 1;
  
  }
  
  </code></pre>
  
  <p>And call it from R like this</p>
  
  <pre><code>library(Rcpp)
  
  sourceCpp("test_order.cpp")
  
  test(3)
  </code></pre>
  
  <p>I get different results when printing. Three different results are the following:</p>
  
  <pre><code>&gt; test(3)
    2.1220e-314            0  6.9531e-310Print 1
  Print 2
  
    2.3044e-314  6.9275e-310  6.9532e-310
    2.1916e-314  2.1916e-314  2.2718e-314
  Print 3
  [1] 1
  </code></pre>
  
  <pre><code>&gt; test(3)
    Print 1
  Print 2
  6.9531e-310  2.3044e-314  4.9407e-324
    6.9532e-310  2.1916e-314  4.9407e-324
              0  6.9275e-310  4.9407e-324
  Print 3
  [1] 1
  
  </code></pre>
  
  <pre><code>&gt; test(3)
    6.9531e-310  2.3044e-314  4.9407e-324
    6.9532e-310  2.1916e-314  4.9407e-324
              0  6.9275e-310  4.9407e-324Print 1
  Print 2
  
  [1]Print 3
   1
  </code></pre>