OpenMP and smart pointers
<p>I'm trying to parallelize what is equivalent to following code</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;boost/shared_ptr.hpp&gt;
using namespace std;

typedef boost::shared_ptr&lt;int&gt; intPtr;

int main(int argc, char **argv) {

  intPtr foo = intPtr(new int(-1));

  for(int i = 0; i&lt;10; i++){

    *foo = i;

    cout &lt;&lt; *foo &lt;&lt; " ";

  }

}
</code></pre>

<p>For reasons that are beyond my control, and that cannot be appreciated in the example, I need to stick to the shared pointer. This is my current attempt:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;boost/shared_ptr.hpp&gt;
using namespace std;

typedef boost::shared_ptr&lt;int&gt; intPtr;

int main(int argc, char **argv) {

  intPtr foo = intPtr(new int(-1));

  bool firstOfThread = true;
  int fooPriv = *foo;

#pragma omp parallel for num_threads(2) firstprivate(firstOfThread,fooPriv,foo)
  for(int i = 0; i&lt;10; i++){

    if(firstOfThread){
      foo.reset(&amp;fooPriv);
    }

    *foo = i;

#pragma omp critical
    cout &lt;&lt; *foo  &lt;&lt; " ";


    firstOfThread=false;
  }

}
</code></pre>

<p>This compiles and prints the 0-9 series, but it is giving me <code>glibc detected</code> errors. In the past, I've worked with bare pointers successfully using the same strategy, with <code>foo=&amp;fooPriv;</code> instead <code>foo.reset(&amp;fooPriv);</code>, but there is something I'm missing here. I suspect it has something to be with the extra pointer inside <code>foo</code> that counts the number of calls, so I'm not correctly making a "deep-copy", as it is mentioned in other question/forums, but I haven't been able to solve it. Any suggestions?</p>

<p>EDIT: The output of the program is sometimes this:</p>

<pre><code>*** glibc detected *** omp-test: free(): invalid pointer: 0x00007fff5da6a7bc ***
0 6 1 7 2 8 3 9 4 10 5 Aborted (core dumped)
</code></pre>

<p>And sometimes this:</p>

<pre><code>*** glibc detected *** omp-test: free(): invalid pointer: 0x00007fffc210277c ***
======= Backtrace: =========
/lib64/libc.so.6[0x3853875e66]
/lib64/libc.so.6[0x385387897a]
omp-test[0x401bc9]
omp-test(main+0xab)[0x401ccb]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x385381ed5d]
omp-test[0x4018e9]
======= Memory map: ========
00400000-00403000 r-xp 00000000 00:15 9568302                            /data/disk01/home/imc/sandbox/scripts/aerie-apps/liff-HealpixSigFluxMapGen/omp-test
00602000-00603000 rw-p 00002000 00:15 9568302                            /data/disk01/home/imc/sandbox/scripts/aerie-apps/liff-HealpixSigFluxMapGen/omp-test
01339000-017f9000 rw-p 00000000 00:00 0                                  [heap]
3853000000-3853020000 r-xp 00000000 fd:00 2359298                        /lib64/ld-2.12.so
385321f000-3853220000 r--p 0001f000 fd:00 2359298                        /lib64/ld-2.12.so
3853220000-3853221000 rw-p 00020000 fd:00 2359298                        /lib64/ld-2.12.so
3853221000-3853222000 rw-p 00000000 00:00 0 
3853400000-3853402000 r-xp 00000000 fd:00 2359303                        /lib64/libdl-2.12.so
3853402000-3853602000 ---p 00002000 fd:00 2359303                        /lib64/libdl-2.12.so
3853602000-3853603000 r--p 00002000 fd:00 2359303                        /lib64/libdl-2.12.so
3853603000-3853604000 rw-p 00003000 fd:00 2359303                        /lib64/libdl-2.12.so
3853800000-385398a000 r-xp 00000000 fd:00 2359302                        /lib64/libc-2.12.so
385398a000-3853b8a000 ---p 0018a000 fd:00 2359302                        /lib64/libc-2.12.so
3853b8a000-3853b8e000 r--p 0018a000 fd:00 2359302                        /lib64/libc-2.12.so
3853b8e000-3853b8f000 rw-p 0018e000 fd:00 2359302                        /lib64/libc-2.12.so
3853b8f000-3853b94000 rw-p 00000000 00:00 0 
3853c00000-3853c17000 r-xp 00000000 fd:00 2359314                        /lib64/libpthread-2.12.so
3853c17000-3853e17000 ---p 00017000 fd:00 2359314                        /lib64/libpthread-2.12.so
3853e17000-3853e18000 r--p 00017000 fd:00 2359314                        /lib64/libpthread-2.12.so
3853e18000-3853e19000 rw-p 00018000 fd:00 2359314                        /lib64/libpthread-2.12.so
3853e19000-3853e1d000 rw-p 00000000 00:00 0 
3854000000-3854083000 r-xp 00000000 fd:00 2359316                        /lib64/libm-2.12.so
3854083000-3854282000 ---p 00083000 fd:00 2359316                        /lib64/libm-2.12.so
3854282000-3854283000 r--p 00082000 fd:00 2359316                        /lib64/libm-2.12.so
3854283000-3854284000 rw-p 00083000 fd:00 2359316                        /lib64/libm-2.12.so
3854400000-3854407000 r-xp 00000000 fd:00 2359320                        /lib64/librt-2.12.so
3854407000-3854606000 ---p 00007000 fd:00 2359320                        /lib64/librt-2.12.so
3854606000-3854607000 r--p 00006000 fd:00 2359320                        /lib64/librt-2.12.so
3854607000-3854608000 rw-p 00007000 fd:00 2359320                        /lib64/librt-2.12.so
3854800000-3854815000 r-xp 00000000 fd:00 2359336                        /lib64/libz.so.1.2.3
3854815000-3854a14000 ---p 00015000 fd:00 2359336                        /lib64/libz.so.1.2.3
3854a14000-3854a15000 r--p 00014000 fd:00 2359336                        /lib64/libz.so.1.2.3
3854a15000-3854a16000 rw-p 00015000 fd:00 2359336                        /lib64/libz.so.1.2.3
3855400000-3855415000 r-xp 00000000 fd:00 1725449                        /usr/lib64/libgomp.so.1.0.0
3855415000-3855614000 ---p 00015000 fd:00 1725449                        /usr/lib64/libgomp.so.1.0.0
3855614000-3855615000 rw-p 00014000 fd:00 1725449                        /usr/lib64/libgomp.so.1.0.0
3855c00000-3855c16000 r-xp 00000000 fd:00 2359508                        /lib64/libresolv-2.12.so
3855c16000-3855e16000 ---p 00016000 fd:00 2359508                        /lib64/libresolv-2.12.so
3855e16000-3855e17000 r--p 00016000 fd:00 2359508                        /lib64/libresolv-2.12.so
3855e17000-3855e18000 rw-p 00017000 fd:00 2359508                        /lib64/libresolv-2.12.so
3855e18000-3855e1a000 rw-p 00000000 00:00 0 
3857800000-3857816000 r-xp 00000000 fd:00 2373979                        /lib64/libnsl-2.12.so
3857816000-3857a15000 ---p 00016000 fd:00 2373979                        /lib64/libnsl-2.12.so
3857a15000-3857a16000 r--p 00015000 fd:00 2373979                        /lib64/libnsl-2.12.so
3857a16000-3857a17000 rw-p 00016000 fd:00 2373979                        /lib64/libnsl-2.12.so
3857a17000-3857a19000 rw-p 00000000 00:00 0 
3857c00000-3857c02000 r-xp 00000000 fd:00 2373980                        /lib64/libutil-2.12.so
3857c02000-3857e01000 ---p 00002000 fd:00 2373980                        /lib64/libutil-2.12.so
3857e01000-3857e02000 r--p 00001000 fd:00 2373980                        /lib64/libutil-2.12.so
3857e02000-3857e03000 rw-p 00002000 fd:00 2373980                        /lib64/libutil-2.12.so
385b800000-385b898000 r-xp 00000000 fd:00 1711172                        /usr/lib64/libfreetype.so.6.3.22
385b898000-385ba97000 ---p 00098000 fd:00 1711172                        /usr/lib64/libfreetype.so.6.3.22
385ba97000-385ba9d000 rw-p 00097000 fd:00 1711172                        /usr/lib64/libfreetype.so.6.3.22
385f400000-385f410000 r-xp 00000000 fd:00 2361304                        /lib64/libbz2.so.1.0.4
385f410000-385f60f000 ---p 00010000 fd:00 2361304                        /lib64/libbz2.so.1.0.4
385f60f000-385f611000 rw-p 0000f000 fd:00 2361304                        /lib64/libbz2.so.1.0.4
38b0800000-38b0816000 r-xp 00000000 fd:00 2366780                        /lib64/libgcc_s-4.4.7-20120601.so.1
38b0816000-38b0a15000 ---p 00016000 fd:00 2366780                        /lib64/libgcc_s-4.4.7-20120601.so.1
38b0a15000-38b0a16000 rw-p 00015000 fd:00 2366780                        /lib64/libgcc_s-4.4.7-20120601.so.1
38b0c00000-38b0ce8000 r-xp 00000000 fd:00 1732781                        /usr/lib64/libstdc++.so.6.0.13
38b0ce8000-38b0ee8000 ---p 000e8000 fd:00 1732781                        /usr/lib64/libstdc++.so.6.0.13
38b0ee8000-38b0eef000 r--p 000e8000 fd:00 1732781                        /usr/lib64/libstdc++.so.6.0.13
38b0eef000-38b0ef1000 rw-p 000ef000 fd:00 1732781                        /usr/lib64/libstdc++.so.6.0.13
38b0ef1000-38b0f06000 rw-p 00000000 00:00 0 
7ffbb8000000-7ffbb8021000 rw-p 00000000 00:00 0 0 6 1 7 2 8 3 9 4 10 5 Aborted (core dumped)
</code></pre>