我正在将TradeStation EasyLanguage指示器代码转换为C++动态链接库。使用TradeStation API可以访问C++ DLL中的市场数据,如下所示:
double currentBarDT = pELObject->DateTimeMD[iDataNumber]->AsDateTime[0];我的问题是:
在C++中,是否有可能以某种方式‘监视’或‘侦听’变量'currentBarDT‘的值何时发生更改/更新?我想使用值的变化作为触发器来生成一个带有Boost.Signals2的信号。
发布于 2013-07-21 19:48:47
您可以使用适合您需要的条件变量。
http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all
在信号中你更新了你的市场数据(i)
在等待过程中,您将条件变量放在i上(例如,股票是否低于某一水平)
告诉我,如果你需要更多的信息,我可以详细说明,让它更明确。
#include <stdlib.h> /* srand, rand */
#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>
#include <atomic>
std::condition_variable cv;
std::mutex cv_m;
double StockPrice;//price of the stock
std::atomic<int> NbActiveThreads=0;//count the number of active alerts to the stock market
void waits(int ThreadID, int PriceLimit)
{
std::unique_lock<std::mutex> lk(cv_m);
cv.wait(lk, [PriceLimit]{return StockPrice >PriceLimit ;});
std::cerr << "Thread "<< ThreadID << "...Selling stock.\n";
--NbActiveThreads;
}
void signals()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cerr << "GettingPrice "<<std::endl;
std::unique_lock<std::mutex> lk(cv_m);
/* generate secret number between 1 and 10: */
StockPrice = rand() % 100 + 1;
std::cerr << "Price =" << StockPrice << std::endl;
cv.notify_all();//updates the price and sell all the stocks if needed
if (NbActiveThreads==0)
{
std::cerr <<"No more alerts "<<std::endl;
return;
}
}
}
int main()
{
NbActiveThreads=3;
std::thread t1(waits,1,20), t2(waits,2,40), t3(waits,3,95), t4(signals);
t1.join();
t2.join();
t3.join();
t4.join();
return 0;
}希望这能有所帮助
发布于 2020-01-17 20:21:48
您可以创建某种存储类并在调用通知程序中实现operator=。可以使用模板,但您需要在标题中保留规范。也可以创建类似的接口并覆盖通知程序调用。
https://stackoverflow.com/questions/16125713
复制相似问题