Note that there are some explanatory texts on larger screens.

plurals
  1. POstl map simultaneous write sample code
    primarykey
    data
    text
    <p>I want to simulate the abnormal behavior of stl map in simultaneous write condition. Here I am using a single map and simultaneously inserting data from multiple threads. As we are using only one map object, hence it should not allow. Please go through the following sample code</p> <pre><code> #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;iterator&gt; #include &lt;algorithm&gt; extern "C" { #include &lt;pthread.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; } using namespace std; //functor to print map struct PrintMp { void operator()(pair&lt;int,int&gt; pr) { cout&lt;&lt;pr.first&lt;&lt;" =&gt; "&lt;&lt;pr.second&lt;&lt;endl; } }; //thread 1 //inserts continuous no from 0 to 4999 void* ins_th1(void *arg) { map&lt;int, int&gt; *mp = static_cast&lt;map&lt;int, int&gt;* &gt;(arg); for(int i =0 ; i&lt;5000; i++) mp-&gt;insert(pair&lt;int,int&gt;(i, i+1000)); return NULL; } //thread 1 //inserts continuous no from 0 to 4999 void* ins_th2(void *arg) { map&lt;int, int&gt; *mp = static_cast&lt;map&lt;int,int&gt;* &gt;(arg); for(int i=5000; i&lt;10000; i++) mp-&gt;insert(pair&lt;int, int&gt;(i, i+2000)); return NULL; } int main() { typedef map&lt;int, int&gt; IntMapType; IntMapType mp; PrintMp MpPrintObj; int rc; pthread_t th1, th2; //thread 1 creation rc = pthread_create(&amp;th1, NULL, ins_th1, static_cast&lt;void*&gt;(&amp;mp)); if ( rc != 0) { cerr&lt;&lt;strerror(rc)&lt;&lt;"in thread1"&lt;&lt;endl; exit(EXIT_FAILURE); } //thread 2 creation rc = pthread_create(&amp;th2, NULL, ins_th2, static_cast&lt;void*&gt;(&amp;mp)); if(rc!=0) { cerr&lt;&lt;strerror(rc)&lt;&lt;"in thread2"&lt;&lt;endl; exit(EXIT_FAILURE); } //lets wait for the thread to finish rc = pthread_join(th1, NULL); if ( rc != 0) { cerr&lt;&lt;strerror(rc)&lt;&lt;"join failure for thread1"&lt;&lt;endl; exit(EXIT_FAILURE); } rc = pthread_join(th2, NULL); if ( rc != 0) { cerr&lt;&lt;strerror(rc)&lt;&lt;"join failure for thread2"&lt;&lt;endl; exit(EXIT_FAILURE); } cout&lt;&lt;"Map data"&lt;&lt;endl; //now print it for_each(mp.begin(), mp.end(), MpPrintObj); cout&lt;&lt;endl; return 0; } </code></pre> <p>But it doesn't work. Can anyone suggest me some approach ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload