gpt4 book ai didi

c++ - QuantLib 多线程/并发

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:50:33 27 4
gpt4 key购买 nike

我对 QuantLib 还很陌生,还不知道源代码的所有来龙去脉,但我试图测试几个选项的 NPV 的简单多线程计算,但遇到了运行时错误。这是我的测试代码,它刚刚从 QL 附带的 EquityOpiton 示例扩展而来。

// 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 中被大量使用,我不确定是哪个(些)导致了这个问题。

最佳答案

您似乎忘记在每个案例后添加中断。尝试以下

 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;
}

关于c++ - QuantLib 多线程/并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16612816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com