-3
unordered_map<std::string,unordered_map<std::string, std::string> >* storing_vars;

我在作用域中声明的作用域中有这个变量。

这是在构造函数中声明的。

this->storing_vars =  new unordered_map<std::string,unordered_map<std::string, std::string> >();

为了初始化它。

然后我所做的是我的 BackgroundWorker 一遍又一遍地调用一个函数

for(int i2 = 0; i2 < 30; i2++){



                int index_pos_curr = i2;


                //Start the Threads HERE


                this->backgroundWorker2 = gcnew System::ComponentModel::BackgroundWorker;
                this->backgroundWorker2->WorkerReportsProgress = true;
                this->backgroundWorker2->WorkerSupportsCancellation = true;


                //this->backgroundWorker2->FieldSetter(L"std::string",L"test","damnnit");

                backgroundWorker2->DoWork += gcnew DoWorkEventHandler( this, &MainFacebook::backgroundWorker2_DoWork );
                backgroundWorker2->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainFacebook::backgroundWorker2_RunWorkerCompleted );
                backgroundWorker2->ProgressChanged += gcnew ProgressChangedEventHandler( this, &MainFacebook::backgroundWorker2_ProgressChanged );
                backgroundWorker2->RunWorkerAsync(index_pos_curr);
                Sleep(50); //THE PROBLEM IS HERE, IF I COMMENT THIS OUT it won't work, that's probably because there are a lot of functions trying to add values in the same variable (even though the indexes are differents in each call)
            }

完成后,它会调用 DoWork 函数

void backgroundWorker2_DoWork(Object^ sender, DoWorkEventArgs^ e ){
             BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);         
             e->Result = SendThem( safe_cast<Int32>(e->Argument), worker, e );          

        }

int SendThem(int index){

            stringstream st;
            st << index;

            //...
            (*this->storing_vars)[st.str()]["index"] =  "testing1";
            (*this->storing_vars)[st.str()]["rs"] = "testing2";
            return 0;
}

当我在该行中添加注释时Sleep(50),我认为问题在于,由于后台线程调用相同的函数,因此在多次调用时存储数据可能会出现问题,甚至可能没有等待其他存储到完成后,它会导致“xhash.h”文件中出现错误,该错误已通过 using 进行清理Sleep(50),但我无法使用这些错误,因为它会冻结我的 UI,而且 50 毫秒是我假设它已经存储的时间变量值,但是如果在较慢的计算机上需要更长的时间怎么办?这不是正确的方法。

我该怎么做才能解决这个问题?

我希望能够在不使用 SLEEP 的情况下更新 unordered_map

提前致谢。

4

3 回答 3

3

unordered_map您一次只能从一个线程修改标准库容器(包括但不限于)。解决方案是使用临界区、互斥体、锁来同步访问。如果您不知道这些是什么,那么您需要在尝试创建多个线程之前知道。

没有如果、但是或为什么。

如果您有多个线程,则需要同步它们的机制,以序列化对共享数据的访问。常见的同步机制就是上面提到的那些,所以去查一下。

于 2012-04-07T21:30:54.680 回答
2

经过这么多票否决后,我实际上开始寻找互斥锁,人们在这里谈论,过了一段时间我发现它真的很容易使用。这是我的同事告诉我的正确方法。谢谢大家的帮助=D

在这里我做了什么,我只需要添加

//Declare the Mutex
static Mutex^ mut = gcnew Mutex;

//then inside the function called over and over again I used mutex
mut->WaitOne();
//Declare/Update the variables
mut->ReleaseMutex();
//Then I release it.

效果很好,谢谢大家的帮助和批评。哈哈

于 2012-04-08T20:31:17.510 回答
-4

我通过预定义我想使用的 unordered_map 的索引找到了一种解决方案,问题只是创建索引,多线程更新似乎没问题。

for(int i2 = 0; i2 < 30; i2++){



                int index_pos_curr = i2;


                //Start the Threads HERE


                this->backgroundWorker2 = gcnew System::ComponentModel::BackgroundWorker;
                this->backgroundWorker2->WorkerReportsProgress = true;
                this->backgroundWorker2->WorkerSupportsCancellation = true;
                backgroundWorker2->DoWork += gcnew DoWorkEventHandler( this, &MainFacebook::backgroundWorker2_DoWork );
                backgroundWorker2->RunWorkerCompleted += gcnew RunWorkerCompletedEventHandler( this, &MainFacebook::backgroundWorker2_RunWorkerCompleted ); stringstream st; st << index_pos_curr;

                (*this->storing_vars)[st.str()]["index"] = ""; 
               //This ^^^^^ will initialize it and then in the BackgroundWorker will only update, this way it doesn't crash. :)

                backgroundWorker2->ProgressChanged += gcnew ProgressChangedEventHandler( this, &MainFacebook::backgroundWorker2_ProgressChanged );
                backgroundWorker2->RunWorkerAsync(index_pos_curr);
                Sleep(50); //THE PROBLEM IS HERE, IF I COMMENT THIS OUT it won't work, that's probably because there are a lot of functions trying to add values in the same variable (even though the indexes are differents in each call)
            }
于 2012-04-07T21:21:22.057 回答