unordered_map<std::string,unordered_map<std::string, std::string> >* storing_vars;
I have this variable in the scope declared in the scope.
This is declared in the constructor.
this->storing_vars = new unordered_map<std::string,unordered_map<std::string, std::string> >();
in order to initialize it.
Then what I do is call a function over and over again by my 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)
}
After this is done it calls the DoWork Function
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;
}
as I added the comment in the Sleep(50) line, I believe the problem is that since the thread in the background call the same function, it has a problem to store the data when it’s called a lot of times probably not even waiting for the other storing to finish, it’s causing an error in the “xhash.h” file, an error that is sanitized by using Sleep(50), but I can’t use those because it freezes my UI and also 50 miliseconds is the time I’m assuming it already stored the variable value, but what if it takes longer in slower computers? it’s not the right approach.
How do I do to fix that?
I want to be able to UPDATE the unordered_map WITHOUT the use of SLEEP
Thanks in advance.
After so many votes down I actually started to look for the Mutex, people were talking about here, after a while I find out that it’s really simple to use. and it’s the correct way as my fellows here told me. Thank you all for the help =D
Here what I did, I just had to add
It works perfectly, Thank you all for the helps and criticism. haha