繁体   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