C++ C++;监视变量的更改值

C++ C++;监视变量的更改值,c++,object,state,tradestation,C++,Object,State,Tradestation,我把一个贸易站易语言指示符代码转换成一个C++ DLL。使用TrimeStayAPI可以访问C++ DLL中的市场数据,如: double currentBarDT = pELObject->DateTimeMD[iDataNumber]->AsDateTime[0]; 我的问题是: 当变量“CurrutBARDT”的值改变/更新时,C++有可能“不知”或“听”吗?我想使用值的变化作为触发器来生成带有Boost.Signals2的信号。您可以使用适合您需要的条件变量 在您更新市场

我把一个贸易站易语言指示符代码转换成一个C++ DLL。使用TrimeStayAPI可以访问C++ DLL中的市场数据,如:

double currentBarDT = pELObject->DateTimeMD[iDataNumber]->AsDateTime[0];
我的问题是:


当变量“CurrutBARDT”的值改变/更新时,C++有可能“不知”或“听”吗?我想使用值的变化作为触发器来生成带有Boost.Signals2的信号。

您可以使用适合您需要的条件变量

在您更新市场数据的信号中(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;
}
#包括/*srand,兰德*/
#包括
#包括
#包括
#包括
#包括
std::条件变量cv;
std::mutex cv_m;
双重股价//股票价格
std::原子NbActiveThreads=0//统计向股市发出的活动警报的数量
void等待(int-ThreadID、int-PriceLimit)
{
std::唯一锁定lk(cv_m);
cv.wait(lk,[PriceLimit]{返回股票价格>PriceLimit;});

std::cerr您可以创建某种类型的存储类,并在calls notifier中实现
operator=
。可以使用模板,但需要在标题中保留规范。也可以创建类似的接口并覆盖notifier调用。

感谢您提供的代码。我将仔细查看。我计划使用的原则是
pELObject->DateTimeMD[iDataNumber]->AsDateTime[0];
指向隐藏在TradeStation API中的市场数据DateTime值。当该DateTime值增加时,即时间以指定的间隔移动,则会广播一个信号以执行进一步的计算。确定后,需要将时间存储在变量中(而不是股票价格)和条件变量。如果你喜欢答案,你能接受吗?它同样有效。这将帮助其他人(并升级我的统计数据:-)