Frage

I am fairly new to QuantLib and don't yet know all the ins and outs of the source but I was trying to test out a simple multi threaded calculation of several option's NPVs and am getting runtime errors. Here's my test code which is just expanded from the EquityOpiton example included with QL.

// 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();

What exactly is causing these runtime errors and how can I fix it? I'm guessing it has something to do with the shared pointers but they are used pretty heavily throughout QL I've noticed and I'm not sure which one(s) are causing the issue.

War es hilfreich?

Lösung

It seems like you forget to add break after each case. Try following

 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;
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top