mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 12:00:04 +00:00 
			
		
		
		
	Use boost::asio for asynchronous processing
This commit is contained in:
		
							parent
							
								
									09064c0be0
								
							
						
					
					
						commit
						bb7793d309
					
				
					 4 changed files with 57 additions and 3 deletions
				
			
		|  | @ -3,30 +3,66 @@ | |||
| #include <scwx/qt/manager/resource_manager.hpp> | ||||
| #include <scwx/qt/manager/settings_manager.hpp> | ||||
| #include <scwx/util/logger.hpp> | ||||
| #include <scwx/util/threads.hpp> | ||||
| 
 | ||||
| #include <aws/core/Aws.h> | ||||
| #include <boost/asio.hpp> | ||||
| #include <spdlog/spdlog.h> | ||||
| #include <QApplication> | ||||
| 
 | ||||
| static const std::string logPrefix_ = "scwx::main"; | ||||
| static const auto        logger_    = scwx::util::Logger::Create(logPrefix_); | ||||
| 
 | ||||
| int main(int argc, char* argv[]) | ||||
| { | ||||
|    // Initialize logger
 | ||||
|    scwx::util::Logger::Initialize(); | ||||
|    spdlog::set_level(spdlog::level::debug); | ||||
| 
 | ||||
|    QCoreApplication::setApplicationName("Supercell Wx"); | ||||
| 
 | ||||
|    // Start the io_context main loop
 | ||||
|    boost::asio::io_context& ioContext = scwx::util::io_context(); | ||||
|    auto                     work      = boost::asio::make_work_guard(ioContext); | ||||
|    boost::asio::thread_pool threadPool {4}; | ||||
|    boost::asio::post(threadPool, | ||||
|                      [&]() | ||||
|                      { | ||||
|                         while (true) | ||||
|                         { | ||||
|                            try | ||||
|                            { | ||||
|                               ioContext.run(); | ||||
|                               break; // run() exited normally
 | ||||
|                            } | ||||
|                            catch (std::exception& ex) | ||||
|                            { | ||||
|                               // Log exception and continue
 | ||||
|                               logger_->error(ex.what()); | ||||
|                            } | ||||
|                         } | ||||
|                      }); | ||||
| 
 | ||||
|    // Initialize AWS SDK
 | ||||
|    Aws::SDKOptions awsSdkOptions; | ||||
|    Aws::InitAPI(awsSdkOptions); | ||||
| 
 | ||||
|    // Initialize application
 | ||||
|    scwx::qt::config::RadarSite::Initialize(); | ||||
|    scwx::qt::manager::SettingsManager::Initialize(); | ||||
|    scwx::qt::manager::ResourceManager::PreLoad(); | ||||
| 
 | ||||
|    // Run Qt main loop
 | ||||
|    QApplication               a(argc, argv); | ||||
|    scwx::qt::main::MainWindow w; | ||||
|    w.show(); | ||||
|    int result = a.exec(); | ||||
| 
 | ||||
|    // Gracefully stop the io_context main loop
 | ||||
|    work.reset(); | ||||
|    threadPool.join(); | ||||
| 
 | ||||
|    // Shutdown AWS SDK
 | ||||
|    Aws::ShutdownAPI(awsSdkOptions); | ||||
| 
 | ||||
|    return result; | ||||
|  |  | |||
|  | @ -1,15 +1,17 @@ | |||
| #include <future> | ||||
| #include <boost/asio/io_context.hpp> | ||||
| #include <boost/asio/post.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace util | ||||
| { | ||||
| 
 | ||||
| boost::asio::io_context& io_context(); | ||||
| 
 | ||||
| template<class F> | ||||
| void async(F&& f) | ||||
| { | ||||
|    auto future = std::make_shared<std::future<void>>(); | ||||
|    *future     = std::async(std::launch::async, [future, f]() { f(); }); | ||||
|    boost::asio::post(io_context(), f); | ||||
| } | ||||
| 
 | ||||
| } // namespace util
 | ||||
|  |  | |||
							
								
								
									
										15
									
								
								wxdata/source/scwx/util/threads.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								wxdata/source/scwx/util/threads.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,15 @@ | |||
| #include <scwx/util/threads.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace util | ||||
| { | ||||
| 
 | ||||
| boost::asio::io_context& io_context() | ||||
| { | ||||
|    static boost::asio::io_context ioContext {}; | ||||
|    return ioContext; | ||||
| } | ||||
| 
 | ||||
| } // namespace util
 | ||||
| } // namespace scwx
 | ||||
|  | @ -54,6 +54,7 @@ set(SRC_UTIL source/scwx/util/float.cpp | |||
|              source/scwx/util/rangebuf.cpp | ||||
|              source/scwx/util/streams.cpp | ||||
|              source/scwx/util/time.cpp | ||||
|              source/scwx/util/threads.cpp | ||||
|              source/scwx/util/vectorbuf.cpp) | ||||
| set(HDR_WSR88D include/scwx/wsr88d/ar2v_file.hpp | ||||
|                include/scwx/wsr88d/level3_file.hpp | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat