2015-08-11 7 views
13

मैं अपने एल्गोरिदम समानांतर प्रक्रिया के लिए std::futures का उपयोग कर रहा हूं। मैं जानकारी को पारस्परिक रूप से अनन्य पूल में विभाजित करता हूं और फिर प्रत्येक पूल पर अपने ही धागे में एक ही ऑपरेशन करता हूं। कोड इस तरह दिखता है:सी ++ वायदा समानांतर प्रसंस्करण

class Processor 
{ 
public: 
    Processor(const std::string &strVal) : m_strVal(strVal) 
    { 
    } 

    std::string GetVal() const {return m_strVal;} 

    std::vector<std::string> Do() 
    { 
     // do some processing - this can throw an exception 
    } 

private: 
    std::string m_strVal; 
}; 

class ParallelAlgo 
{ 
private: 
    std::vector<std::string> m_vecMasterResults; 

public: 

    ProcessingFunction(const std::vector<std::string> &vecInfo) 
    { 
     // vecInfo holds mutually exclusive pools 

     std::vector<std::future<std::vector<std::string> > > vecFutures(vecInfo.size()); 

     try 
     { 
      for (auto n = 0 ; n < vecInfo.size() ; n++) 
      { 
       vecFuture[n] = std::async(std::launch::async, &ParallelAlgo::WorkFunc, vecInfo[n].GetVal()); 
      } 

      for (auto it = vecFutures.begin() ; it != vecFutures.end() ; ++it) 
      { 
       std::vector<std::string> RetVal = it->get(); 
       m_MasterResults.insert(m_MasterResults.begin(), RetVal.begin(), RetVal.end()); 
       vecFutures.erase(it); 
      } 
     } 
     catch (exception &e) 
     { 
      for (auto it = vecFutures.begin() ; it != vecFuture.end() ; ++it) 
      { 
       // race condition? 
       if (it->valid()) 
       { 
        it->wait_for(std::chrono::second(0)); 
       } 
      } 
     } 
    } 

    std::vector<std::string> ParallelAlgo::WorkFunc(const std::string &strVal) 
    { 
     Processor _Proccessor(strVal); 
     return _Processor.Do(); 
    } 
}; 

मेरे सवाल यह है कि स्थिति से निपटने के लिए एक अपवाद Processor:Do() में फेंक दिया जाता है जब है? वर्तमान में मैं future का उपयोग करके अपवाद पकड़ता हूं, और उसके बाद प्रत्येक future के लिए शून्य सेकंड प्रतीक्षा करें जो समाप्त नहीं हुआ है; यह ठीक है - ये धागे बस समाप्त हो जाएंगे और प्रसंस्करण पूरा नहीं होगा। हालांकि, क्या मैंने कैच ब्लॉक में दौड़ की स्थिति नहीं पेश की है। futurevalid() और wait_for() पर कॉल के बीच समाप्त हो सकता है, या यह चिंता नहीं है क्योंकि मैं इन अपूर्ण वायदा पर get() पर कॉल नहीं कर रहा हूं?

+1

एक समाप्त 'थ्रेड' पर 'wait_for' को कॉल करना ठीक उसी तरह काम करता है जैसा कि कोई उम्मीद करेगा, यह तुरंत लौटता है। मुझे समस्या नहीं दिख रही है। – nwp

+0

जैसा कि आप अपवाद के साथ कुछ भी नहीं करते हैं, क्या आप अधूरे धागे के बारे में चिंता नहीं कर सकते? 'Std :: future' के विनाशक, यदि आवश्यक हो, तो एसिंक प्रोसेसिंग फिनिश तक अवरुद्ध हो जाएंगे, लेकिन जहां तक ​​मैं समझता हूं कि आप यही चाहते हैं। –

+1

उदाहरण कोड में एक बग है। 'VecFutures.erase (इसे) पर कॉल करना; 'इसे' अमान्य कर देगा और इसके लिए आगे लूप में इसका उपयोग यूबी होगा। – DaBrain

उत्तर

3

valid पर कॉल केवल एक ही साझा स्थिति है, जो तब भी जांचता है जब एक संबंधित थ्रेड के लिए अभी भी सत्य होगा, जब तक कि आप get पर कॉल न करें।

वहां कोई दौड़ स्थिति नहीं है।

संबंधित मुद्दे