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. 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(); , if i use for loop() or other function then it will not freeze.
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)
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.