rust-windowing / winit

Window handling library in pure Rust
https://docs.rs/winit/
Apache License 2.0
4.88k stars 911 forks source link

Windows: set_cursor_visible blocks forever when called from thread #3983

Closed spearman closed 3 weeks ago

spearman commented 3 weeks ago

Description

When calling set_cursor_visible from a thread other than main, the code is blocked forever waiting on recv(): https://github.com/rust-windowing/winit/blob/v0.30.5/src/platform_impl/windows/window.rs#L472

use std;
use winit;
use winit::event_loop::EventLoop;
use winit::window::WindowAttributes;

fn main() {
  let event_loop = EventLoop::new().unwrap();
  #[allow(deprecated)]
  let window = event_loop.create_window(WindowAttributes::default()).unwrap();
  window.set_cursor_visible(true);
  println!("set cursor visible on main");
  // spawn thread
  let thread_handle = std::thread::spawn(move ||{
    window.set_cursor_visible(true);  // blocks forever
    println!("set cursor visible on thread");
  });
  thread_handle.join().unwrap();
}

Windows version

Microsoft Windows [Version 10.0.19043.928]

Winit version

0.30.5

madsmtm commented 3 weeks ago

Likely cause: You have to be running the event loop. I at least know that this would also block on macOS if you don't do that.

spearman commented 3 weeks ago

That seems to be it, I'll close this