简体   繁体   English

stl map同时写示例代码

[英]stl map simultaneous write sample code

I want to simulate the abnormal behavior of stl map in simultaneous write condition. 我想在同时写入条件下模拟stl映射的异常行为。 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 请通过以下示例代码

    #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;
    }

But it doesn't work. 但这是行不通的。 Can anyone suggest me some approach ? 谁能建议我一些办法?

You are only testing insertion, which may or may not be implemented in a thread safe fashion, for all you know. 就您所知,您仅测试插入,可能以线程安全方式实现或可能不实现插入。 But, your test is not complete. 但是,您的测试尚未完成。 Allow the threads to write the same key into the map , and you will likely experience an error that would not happen without multiple threads. 允许线程将相同的键写入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));

You should test deletion from the map as well. 您还应该测试从map删除。 When I modified your program to populate the map , before launching the threads, and only had the threads remove things from the map , the program live-locked. 当我修改程序以填充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

I tried to implement as you said. 我试着按照你说的去实现。

But despite not using any synchronization mechanism, I am getting the perfect result. 但是,尽管没有使用任何同步机制,但是我得到了完美的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM