C监视变量值的变量

我正在将TradeStation EasyLanguage指标代码转换为C DLL.使用TradeStation API,可以访问C DLL中的市场数据,如下所示:
double currentBarDT = pELObject->DateTimeMD[iDatanumber]->AsDateTime[0];

我的问题是:

当变量’currentBarDT’的值改变/更新时,C是否可能以某种方式“观察”或“监听”?我想使用更改值作为触发器来使用Boost.Signals2生成信号.

解决方法

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

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

希望有所帮助

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...