簡體   English   中英

需要有關 gtkmm 中多線程的幫助

[英]Need Help Regarding multithreading in gtkmm

我對線程不是很糟糕,我需要幫助,

我有一個帶有進度條的 gtkmm 窗口,任務是在后台執行多個 shell 腳本或 shell 命令並相應地更新進度條。 我有一個

button->clicked_signal() {
         thread( [this] { worker->start() } ); 
}  


worker->start() {
      {
         lock_guard(mutex);         // as per i know to safely handle variables 
         progress = 0.0      
     }
      caller->notify();             // i found it on documentation that its for sending signal to window to refresh

   int ret = WEXITSTATUS(system("./my_shell_script with-args"));
  {
     lock_guard(mutex);
     progress = 0.4;
   }
  caller->notify();       // Simmilary i am handling more scripts
} 

問題是“完成”窗口在進程完成之前一直處於凍結狀態。 它只發生在 system() 上; ,如果我使用 for loop() 或其他函數,則它不會凍結。

我嘗試了其他的東西。

worker->executor() {
      int ret = WEXITSTATUS(system("./my_shell_script with-args"));
      {
         lock_guard(mutex);
         progress = 0.4;
       }
        // other executions
 }

worker->start() {
      thread  th1(&worker::executor, this);
      while(true) {
          caller->notify();
          if (stop) break;                             

      this_thread::sleep_for(chrono::milliseconds(120));     
      }

     th1.join();
     caller->notify();
} 

但仍然凍結。 我非常不擅長穿線,

完整代碼可在https://github.com/itsManjeet/opportunity.git BRANCH (0.6.0) 獲得

他們有沒有更好的方法來做這件事

除了主線程之外,您不能從任何線程進行任何 GUI 更改或使用任何其他 GTK 函數。 例如,如果您需要更改進度條,則必須向主線程發出信號來執行此操作,而不是嘗試從工作線程進行更改。 您可以為此使用Glib::Dispatcher

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM