How to separate the update of the egui GUI and background program processes?

Bellow is my code, as you can see, placement of labels and computation are in one large update function.

impl eframe::App for Application {
        fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
            egui::CentralPanel::default().show(ctx, |ui| {
                if self.checks[0] == 0 {
                    let john: Person = self.sim_data.create_person(0);
                    let john2: Person = self.sim_data.create_person(1);
                    self.sim_data.people.push(john);
                    self.sim_data.people.push(john2);
                    self.checks[0] = 1;
                }

                if self.checks[1] != 0 {
                    self.sim_data.update_sim(&self.world_data);

                    for id in 0..self.sim_data.people.len() - 1 {
                        if id < self.sim_data.people.len() && self.sim_data.people[id].love_vec[0] != -1
                            && self.sim_data.people[id].age > 30 * 12
                            || self.sim_data.people[id].stats[0] <= 0.0
                        {
                            self.sim_data.people[id].age = -1;
                        }
                    }

                    self.sim_data.people.retain(|person| person.age != -1);
                    self.checks[1] -= 1;
                }

                egui::CentralPanel::default().show(ctx, |ui| {
                    ui.label(egui::RichText::new(
                        format!("Population: {}", self.sim_data.people.len())).size(125.0));

                    ui.label(egui::RichText::new(
                        format!("Months Passed: {}", MONTHS_OF_POPULATING - self.checks[1])).size(25.0));

                    ui.label(egui::RichText::new(
                        format!("Months left: {}", self.checks[1])).size(15.0));
                });

                egui::TopBottomPanel::bottom("settings").show(ctx, |ui| {
                    egui::CollapsingHeader::new("THEME")
                        .show(ui, |ui| egui::
                        widgets::global_dark_light_mode_buttons(ui));
                });
                // println!("{:?}", self.sim_data.people);
                ctx.request_repaint();
            });
        }
    }

What should i do to decouple the computation code and display code into 2?
I know that I should use threads, but I have never done this before as I am relatively new to rust.
What would be the best implementation of a 2 thread approach?

Thank you :slight_smile:

1 Like

The very abbreviated version is to use spawn in std::thread - Rust to create the thread, and std::sync::mpsc - Rust to communicate between the backend and frontend. The details get fiddly from there: perhaps you want to send the updated state from the backend, perhaps you want to update in place and share it between the threads, perhaps you want to send only diffs and keep the full UX on the UI thread...

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.