diff --git a/hikyuu_cpp/hikyuu/StockManager.cpp b/hikyuu_cpp/hikyuu/StockManager.cpp index a75bec90..dee7096d 100644 --- a/hikyuu_cpp/hikyuu/StockManager.cpp +++ b/hikyuu_cpp/hikyuu/StockManager.cpp @@ -41,11 +41,13 @@ void StockManager::quit() { } StockManager::StockManager() { + m_stockDict_mutex = new std::mutex; m_marketInfoDict_mutex = new std::mutex; m_stockTypeInfo_mutex = new std::mutex; } StockManager::~StockManager() { + delete m_stockDict_mutex; delete m_marketInfoDict_mutex; delete m_stockTypeInfo_mutex; fmt::print("Quit Hikyuu system!\n\n"); @@ -252,6 +254,7 @@ Stock StockManager::getStock(const string& querystr) const { Stock result; string query_str = querystr; to_upper(query_str); + std::lock_guard lock(*m_stockDict_mutex); auto iter = m_stockDict.find(query_str); return (iter != m_stockDict.end()) ? iter->second : result; } @@ -354,6 +357,7 @@ void StockManager::removeTempCsvStock(const string& code) { bool StockManager::loadStock(const Stock& stock) { string market_code(stock.market_code()); to_upper(market_code); + std::lock_guard lock(*m_stockDict_mutex); HKU_ERROR_IF_RETURN(m_stockDict.find(market_code) != m_stockDict.end(), false, "The stock had exist! {}", market_code); m_stockDict[market_code] = stock; @@ -361,6 +365,7 @@ bool StockManager::loadStock(const Stock& stock) { } bool StockManager::loadMarketInfo(const MarketInfo& marketInfo) { + std::lock_guard lock(*m_marketInfoDict_mutex); string market = marketInfo.market(); to_upper(market); HKU_ERROR_IF_RETURN(m_marketInfoDict.find(market) != m_marketInfoDict.end(), false, @@ -370,6 +375,7 @@ bool StockManager::loadMarketInfo(const MarketInfo& marketInfo) { } bool StockManager::loadStockTypeInfo(const StockTypeInfo& stkTypeInfo) { + std::lock_guard lock(*m_stockTypeInfo_mutex); HKU_ERROR_IF_RETURN(m_stockTypeInfo.find(stkTypeInfo.type()) != m_stockTypeInfo.end(), false, "The stockTypeInfo had exist! {}", stkTypeInfo.type()); m_stockTypeInfo[stkTypeInfo.type()] = stkTypeInfo; diff --git a/hikyuu_cpp/hikyuu/StockManager.h b/hikyuu_cpp/hikyuu/StockManager.h index b33f8a4a..13cef437 100644 --- a/hikyuu_cpp/hikyuu/StockManager.h +++ b/hikyuu_cpp/hikyuu/StockManager.h @@ -197,6 +197,7 @@ private: BlockInfoDriverPtr m_blockDriver; StockMapIterator::stock_map_t m_stockDict; // SH000001 -> stock + std::mutex* m_stockDict_mutex; typedef unordered_map MarketInfoMap; mutable MarketInfoMap m_marketInfoDict; diff --git a/hikyuu_cpp/hikyuu/data_driver/BaseInfoDriver.cpp b/hikyuu_cpp/hikyuu/data_driver/BaseInfoDriver.cpp index 9737b345..29e20fec 100644 --- a/hikyuu_cpp/hikyuu/data_driver/BaseInfoDriver.cpp +++ b/hikyuu_cpp/hikyuu/data_driver/BaseInfoDriver.cpp @@ -6,6 +6,8 @@ */ #include +#include "../StockManager.h" +#include "../base/GlobalTaskGroup.h" #include "BaseInfoDriver.h" namespace hku { @@ -68,6 +70,22 @@ bool BaseInfoDriver::loadBaseInfo() { HKU_INFO("Loading stock information..."); HKU_FATAL_IF_RETURN(!_loadStock(), false, "Can't load Stock"); + HKU_INFO("Loading stock weight..."); + auto* tg = getGlobalTaskGroup(); + auto& sm = StockManager::instance(); + std::vector> task_list; + for (auto stock : sm) { + task_list.push_back(tg->submit([=]() mutable { + StockWeightList weightList = this->getStockWeightList( + stock.market(), stock.code(), Datetime::min(), Null()); + stock.setWeightList(weightList); + })); + } + // 权息信息如果不等待加载完毕,在数据加载期间进行计算可能导致复权错误,所以这里需要等待 + for (auto& task : task_list) { + task.get(); + } + return true; } diff --git a/hikyuu_cpp/hikyuu/data_driver/base_info/mysql/MySQLBaseInfoDriver.cpp b/hikyuu_cpp/hikyuu/data_driver/base_info/mysql/MySQLBaseInfoDriver.cpp index a44304be..232de5b7 100644 --- a/hikyuu_cpp/hikyuu/data_driver/base_info/mysql/MySQLBaseInfoDriver.cpp +++ b/hikyuu_cpp/hikyuu/data_driver/base_info/mysql/MySQLBaseInfoDriver.cpp @@ -201,11 +201,7 @@ bool MySQLBaseInfoDriver::_loadStock() { Stock(marketDict[r.marketid], r.code, r.name, r.type, r.valid, startDate, endDate); } - if (sm.loadStock(stock)) { - StockWeightList weightList = - getStockWeightList(marketDict[r.marketid], r.code, Datetime::min(), Null()); - stock.setWeightList(weightList); - } + sm.loadStock(stock); } return true; diff --git a/hikyuu_cpp/hikyuu/data_driver/base_info/sqlite/SQLiteBaseInfoDriver.cpp b/hikyuu_cpp/hikyuu/data_driver/base_info/sqlite/SQLiteBaseInfoDriver.cpp index 45804642..a8849ce8 100644 --- a/hikyuu_cpp/hikyuu/data_driver/base_info/sqlite/SQLiteBaseInfoDriver.cpp +++ b/hikyuu_cpp/hikyuu/data_driver/base_info/sqlite/SQLiteBaseInfoDriver.cpp @@ -148,11 +148,7 @@ bool SQLiteBaseInfoDriver::_loadStock() { Stock(marketDict[r.marketid], r.code, r.name, r.type, r.valid, startDate, endDate); } - if (sm.loadStock(stock)) { - StockWeightList weightList = - getStockWeightList(marketDict[r.marketid], r.code, Datetime::min(), Null()); - stock.setWeightList(weightList); - } + sm.loadStock(stock); } return true;