0

同時書き込み状態で stl マップの異常な動作をシミュレートしたい。ここでは、1 つのマップを使用し、複数のスレッドから同時にデータを挿入しています。マップ オブジェクトを 1 つしか使用していないため、許可しないでください。次のサンプルコードを実行してください

    #include <iostream>
    #include <map>
    #include <iterator>
    #include <algorithm>

    extern "C"
    {
    #include <pthread.h>
    #include <string.h>
    #include <stdlib.h>
    }

    using namespace std;

//functor to print map
    struct PrintMp
    {
       void operator()(pair<int,int> pr)
       {
          cout<<pr.first<<" => "<<pr.second<<endl;
       }
    };
//thread 1
//inserts continuous no from 0 to 4999    
    void* ins_th1(void *arg)
    {
       map<int, int> *mp = static_cast<map<int, int>* >(arg);
       for(int i =0 ; i<5000; i++)
          mp->insert(pair<int,int>(i, i+1000));

       return NULL;
    }

//thread 1
//inserts continuous no from 0 to 4999    
    void* ins_th2(void *arg)
    {
       map<int, int> *mp = static_cast<map<int,int>* >(arg);
       for(int i=5000; i<10000; i++)
          mp->insert(pair<int, int>(i, i+2000));
       return NULL;
    }


    int main()
    {
       typedef map<int, int> IntMapType;

       IntMapType mp;
       PrintMp MpPrintObj;
       int rc;

       pthread_t th1, th2;
    //thread 1 creation
       rc = pthread_create(&th1, NULL, ins_th1, static_cast<void*>(&mp));
       if ( rc != 0)
       {
          cerr<<strerror(rc)<<"in thread1"<<endl;
          exit(EXIT_FAILURE);
       }
    //thread 2 creation
       rc = pthread_create(&th2, NULL, ins_th2, static_cast<void*>(&mp));
       if(rc!=0)
       {
          cerr<<strerror(rc)<<"in thread2"<<endl;
          exit(EXIT_FAILURE);
       }
    //lets wait for the thread to finish
       rc = pthread_join(th1, NULL);
       if ( rc != 0)
       {
          cerr<<strerror(rc)<<"join failure for thread1"<<endl;
          exit(EXIT_FAILURE);
       }

       rc = pthread_join(th2, NULL);
       if ( rc != 0)
       {
          cerr<<strerror(rc)<<"join failure for thread2"<<endl;
          exit(EXIT_FAILURE);
       }

       cout<<"Map data"<<endl;
    //now print it
       for_each(mp.begin(), mp.end(), MpPrintObj);
       cout<<endl;

       return 0;
    }

しかし、うまくいきません。誰かが私にいくつかのアプローチを提案できますか?

4

2 に答える 2

1

あなたが知っているすべての人のために、あなたは挿入をテストしているだけであり、それはスレッドセーフな方法で実装されるかもしれないし、されないかもしれない。しかし、あなたのテストは完了していません。スレッドが同じキーをに書き込むことを許可すると、map複数のスレッドがないと発生しないエラーが発生する可能性があります。

   // ins_th1
   for(int i =0 ; i<10000; i++)
      mp->insert(pair<int,int>(i, i+1000));

   // ins_th2
   for(int i=0; i<10000; i++)
      mp->insert(pair<int, int>(i, i+2000));

からの削除もテストする必要がmapあります。スレッドを起動する前に、プログラムにデータを入力するように変更しmap、スレッドにすべてを削除させるmapと、プログラムはライブロックされました。

   // ins_th1
   for(int i =0 ; i<5000; i++)
      mp->erase(i);

   // ins_th2
   for(int i=5000; i<10000; i++)
      mp->erase(i);

   // near top of main
   for(int i =0 ; i<5000; i++)
      mp.insert(pair<int,int>(i, i+1000));
   for(int i=5000; i<10000; i++)
      mp.insert(pair<int, int>(i, i+2000));
   //... launch threads
于 2012-07-20T21:56:14.703 に答える
0

おっしゃる通り実装してみました。

しかし、同期メカニズムを使用していないにもかかわらず、完璧な結果が得られています。

于 2012-07-27T21:21:19.317 に答える