【发布时间】:2020-09-10 06:07:23
【问题描述】:
我在线程方面不是很差,我需要帮助,
我有一个带有进度条的 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) 获得
他们有没有更好的方法来做这件事
【问题讨论】:
标签: c++ multithreading gtkmm gtkmm3