您可以使用符合您需要的条件变量。
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;
}
希望帮助
感谢您的代码。将查看它。我计划使用的原则是'pELObject-> DateTimeMD [iDataNumber] - > AsDateTime [0];'指向嵌入在TradeStation API中的市场数据日期时间值。当该日期时间增量时,即时间已经移动了指定的时间间隔,则将广播信号以进行进一步的计算。 – GoFaster 2013-07-22 21:05:09
好吧,那么你需要将时间存储在一个变量(而不是股票价格)中,并且条件变量将会存在。如果你喜欢答案,你能接受是否有效。这将有助于其他人(并升级我的数据也:-)) – Gabriel 2013-07-22 21:28:06