2013-05-17 52 views
1

我對QuantLib相當陌生,並且還不知道源代碼的所有來龍去脈,但我試圖測試一個簡單的多線程計算幾個選項的NPV並得到運行時錯誤。這裏是我的測試代碼,它剛剛從QL包含的EquityOpiton示例中擴展而來。QuantLib Multithreading/Concurrecy

// options 
VanillaOption europeanOption(payoff, europeanExercise); 
VanillaOption bermudanOption(payoff, bermudanExercise); 
VanillaOption americanOption(payoff, americanExercise); 

boost::thread_group worker_threads; 
for(int x = 0; x < 3; ++x) 
{ 
    switch (x) 
    { 
    case 0: 
     europeanOption.setPricingEngine(boost::shared_ptr(
      new FDEuropeanEngine(bsmProcess, 
               100,99))); 
     worker_threads.create_thread(boost::bind(&VanillaOption::NPV, &europeanOption)); 
    case 1: 
     bermudanOption.setPricingEngine(boost::shared_ptr(
      new FDBermudanEngine(bsmProcess, 
               100,99))); 
     worker_threads.create_thread(boost::bind(&VanillaOption::NPV, &bermudanOption)); 
    case 2: 
     americanOption.setPricingEngine(boost::shared_ptr(
     new FDAmericanEngine(bsmProcess, 
               100,99))); 
     worker_threads.create_thread(boost::bind(&VanillaOption::NPV, &americanOption)); 
     default: 
     break; 
    } 
} 


worker_threads.join_all(); 

究竟是什麼導致這些運行時錯誤,我該如何解決它?我猜測它與共享指針有關,但在我注意到的整個QL中它們的使用非常嚴重,我不確定哪個引起了這個問題。

回答

1

好像你忘了在每個案例後加上break。嘗試以下內容

switch (x) 
{ 
case 0: 
{ 
    europeanOption.setPricingEngine(boost::shared_ptr(
     new FDEuropeanEngine(bsmProcess, 100,99))); 
    worker_threads.create_thread(boost::bind(&VanillaOption::NPV, &europeanOption) ); 
    break; 
} 
case 1: 
{ 
    opt = &bermudanOption; 
    bermudanOption.setPricingEngine(boost::shared_ptr(
     new FDBermudanEngine(bsmProcess, 
              100,99))); 
    worker_threads.create_thread(boost::bind(&VanillaOption::NPV, &bermudanOption)); 
    break; 
} 
case 2: 
{ 
    americanOption.setPricingEngine(boost::shared_ptr(
    new FDAmericanEngine(bsmProcess, 
              100,99))); 
    worker_threads.create_thread(boost::bind(&VanillaOption::NPV, &americanOption)); 
    break; 
    } 
    default: 
    break; 
} 
+0

derp。你明白了,那就是訣竅......非常感謝讀者和回答。 – user2183336

+0

@ user218336不客氣。您可以通過單擊左側的向上三角形來獲得您認爲有用的答案。 (除了檢查標記對您最有用的答案之外,還提供這個功能。) –