UI操作 解决方案

  1 #include                                                                                                                                                                                                           
  2 #include                                                                                                                                                                                                              
  3 #include                                                                                                                                                                                                              
  4 #include                                                                                                                                                                                                             
  5 #include                                                                                                                                                                                                             
  6 #include                                                                                                                                                                                                            
  7                                                                                                                                                                                                                              
  8                                                                                                                                                                                                                              
  9 std::mutex m;                                                                                                                                                                                                                
 10 std::deque > tasks;                                                                                                                                                                               
 11                                                                                                                                                                                                                              
 12 bool gui_shutdown_message_received();                                                                                                                                                                                        
 13 void get_and_process_gui_message();                                                                                                                                                                                          
 14                                                                                                                                                                                                                              
 15 void gui_thread()                                                                                                                                                                                                            
 16 {                                                                                                                                                                                                                            
 17     while(!gui_shutdown_message_received())                                                                                                                                                                                  
 18     {                                                                                                                                                                                                                        
 19         get_and_process_gui_message();                                                                                                                                                                                       
 20         std::packaged_task task;                                                                                                                                                                                     
 21         {                                                                                                                                                                                                                    
 22             std::lock_guard lk(m);                                                                                                                                                                               
 23             if(tasks.empty())                                                                                                                                                                                                
 24                 continue;                                                                                                                                                                                                    
 25                                                                                                                                                                                                                              
 26             task = std::move(tasks.front());                                                                                                                                                                                 
 27             tasks.pop_front();                                                                                                                                                                                               
 28         }                                                                                                                                                                                                                    
 29         task();                                                                                                                                                                                                              
 30     }                                                                                                                                                                                                                        
 31 }                                                                                                                                                                                                                            
 32                                                                                                                                                                                                                              
 33 std::thread gui_bg_thread(gui_thread);                                                                                                                                                                                       
 34                                                                                                                                                                                                                              
 35 template                                                                                                                                                                                                      
 36 std::future post_task_for_gui_thread(Func f)                                                                                                                                                                           
 37 {                                                                                                                                                                                                                            
 38     std::packaged_task task(f);                                                                                                                                                                                      
 39     std::future res = task.get_future();                                                                                                                                                                               
 40     std::lock_guard lk(m);                                                                                                                                                                                       
 41     tasks.push_back(std::move(task));                                                                                                                                                                                        
 42     return res;                                                                                                                                                                                                              
 43 }                                                                                                                                                                                                                            
 44                                                                                                                                                                                                                              
 45                                                                                                                                                                                                                              
 46 int main(int argc,char** argv)                                                                                                                                                                                               
 47 {                                                                                                                                                                                                                            
 48     std::cout << "-------------------------------------------------------" << std::endl;                                                                                                                                     
 49                                                                                                                                                                                                                              
 50     return 0;                                                                                                                                                                                                                
 51 }                                                                                                                                                                                                                                                           
~            

你可能感兴趣的:(C++)