简体   繁体   English

需要有关 gtkmm 中多线程的帮助

[英]Need Help Regarding multithreading in gtkmm

I am not very bad with threading, i need help,我对线程不是很糟糕,我需要帮助,

I have a gtkmm window with progress bar, task is to execute multiple shell scripts or shell commands in background and update the progress bar accordingly.我有一个带有进度条的 gtkmm 窗口,任务是在后台执行多个 shell 脚本或 shell 命令并相应地更新进度条。 i have a我有一个

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
} 

Issue is that Complete window is freezing until the process finish.问题是“完成”窗口在进程完成之前一直处于冻结状态。 its only happening for system();它只发生在 system() 上; , if i use for loop() or other function then it will not freeze. ,如果我使用 for loop() 或其他函数,则它不会冻结。

I tried other thing.我尝试了其他的东西。

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();
} 

But Still Freezing.但仍然冻结。 I am very bad at threading,我非常不擅长穿线,

complete codes are available at https://github.com/itsManjeet/opportunity.git BRANCH (0.6.0)完整代码可在https://github.com/itsManjeet/opportunity.git BRANCH (0.6.0) 获得

Is their any better way to do this thing他们有没有更好的方法来做这件事

You cannot make any GUI changes or use any other GTK functions from any thread except the main one.除了主线程之外,您不能从任何线程进行任何 GUI 更改或使用任何其他 GTK 函数。 If you need to change a progress bar, for example, then you have to signal the main thread to do it, instead of trying to make the change from the worker thread.例如,如果您需要更改进度条,则必须向主线程发出信号来执行此操作,而不是尝试从工作线程进行更改。 You can use Glib::Dispatcher for this.您可以为此使用Glib::Dispatcher

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM