2013-04-20 39 views
7

我正在將TradeStation EasyLanguage指示器代碼轉換爲C++ DLL。使用TradeStation API有可能進入市場數據在C++ DLL像這樣:用於更改值的C++監視變量

double currentBarDT = pELObject->DateTimeMD[iDataNumber]->AsDateTime[0]; 

我的問題是:

是否有可能在C++中以某種方式「手錶」或「聽」的當變量'currentBarDT'的值已更改/更新?我想用這個值的改變作爲一個觸發器,用Boost.Signals2生成一個信號。

回答

2

您可以使用符合您需要的條件變量。

http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all

在您更新的市場數據(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; 
} 

希望幫助

+0

感謝您的代碼。將查看它。我計劃使用的原則是'pELObject-> DateTimeMD [iDataNumber] - > AsDateTime [0];'指向嵌入在TradeStation API中的市場數據日期時間值。當該日期時間增量時,即時間已經移動了指定的時間間隔,則將廣播信號以進行進一步的計算。 – GoFaster 2013-07-22 21:05:09

+0

好吧,那麼你需要將時間存儲在一個變量(而不是股票價格)中,並且條件變量將會存在。如果你喜歡答案,你能接受是否有效。這將有助於其他人(並升級我的數據也:-)) – Gabriel 2013-07-22 21:28:06