locked
multithreading problem

    Question

  • I need to use something like this

    void MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)

    {

    for (int i = 1; i <= 1000; i++) { create_task([i]()->String ^ { String ^str = i.ToString(); return str; }).then([this](String ^str) { v->Append(str); }); }

    }

     and v is Vector<String^>^

    but I get runtime error. I know it's because Vector is not thread safe. If I use this it will work

    void MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    
    {
    
    for (int i = 1; i <= 1000; i++)
    {
    	create_task([i]()->String ^
    	{
    		String ^str = i.ToString();
    
    		return str;
    	}).then([this](String ^str)
    	{
    		v->Append(str);
    	},task_continuation_context::use_current());
    }
    
    }
    
    

    becasue now Append run in ASTA that is a Single Thread Apartment

    but if I don't want to use UI thread or don't have ASTA like when I am in background task

    What should I do?

    ( according to this I can't create other STAs :

    http://msdn.microsoft.com/en-us/library/windows/apps/hh750290.aspx

    but according to this a process can have any number of STA :

    http://msdn.microsoft.com/en-us/library/windows/apps/ms693344.aspx

    also because of some reasons I don't want to use concurrent_vector )

    )

    Wednesday, October 31, 2012 2:01 PM

Answers