From 0f708dd562269161927e684698ceab85dd08e5bc Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sat, 3 Aug 2024 11:24:02 +0200 Subject: [PATCH] examples/winit_multithread: Make thread persistent again --- examples/winit_multithread.rs | 37 +++++++++++++++-------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/examples/winit_multithread.rs b/examples/winit_multithread.rs index 644d11c..b85da9b 100644 --- a/examples/winit_multithread.rs +++ b/examples/winit_multithread.rs @@ -19,17 +19,16 @@ mod ex { fn render_thread( window: Arc, - surface: Arc>, - do_render: mpsc::Receiver<()>, + do_render: mpsc::Receiver>>, done: mpsc::Sender<()>, ) { loop { println!("waiting for render..."); - if do_render.recv().is_err() { - println!("surface state destroyed"); - // The surface state returned from init_surface below is destroyed. + let Ok(surface) = do_render.recv() else { + println!("main thread destroyed"); + // Main thread is dead. break; - } + }; // Perform the rendering. let mut surface = surface.lock().unwrap(); @@ -74,15 +73,6 @@ mod ex { let context = softbuffer::Context::new(window.clone()).unwrap(); - (window, context) - }, - |_elwt, (window, context)| { - let surface = { - println!("making surface..."); - let surface = softbuffer::Surface::new(context, window.clone()).unwrap(); - Arc::new(Mutex::new(surface)) - }; - // Spawn a thread to handle rendering for this specific surface. The channels will // be closed and the thread will be stopped whenever this surface (the returned // context below) is dropped, so that it can all be recreated again (on Android) @@ -92,15 +82,20 @@ mod ex { println!("starting thread..."); std::thread::spawn({ let window = window.clone(); - let surface = surface.clone(); - move || render_thread(window, surface, do_render, render_done) + move || render_thread(window, do_render, render_done) }); - (surface, start_render, finish_render) + (window, context, start_render, finish_render) + }, + |_elwt, (window, context, _start_render, _finish_render)| { + println!("making surface..."); + Arc::new(Mutex::new( + softbuffer::Surface::new(context, window.clone()).unwrap(), + )) }, ) .with_event_handler(|state, surface, event, elwt| { - let (window, _context) = state; + let (window, _context, start_render, finish_render) = state; elwt.set_control_flow(ControlFlow::Wait); match event { @@ -108,12 +103,12 @@ mod ex { window_id, event: WindowEvent::RedrawRequested, } if window_id == window.id() => { - let Some((_surface, start_render, finish_render)) = surface else { + let Some(surface) = surface else { eprintln!("RedrawRequested fired before Resumed or after Suspended"); return; }; // Start the render and then finish it. - start_render.send(()).unwrap(); + start_render.send(surface.clone()).unwrap(); finish_render.recv().unwrap(); } Event::WindowEvent {