-
Notifications
You must be signed in to change notification settings - Fork 585
Introduce the dedicated windows-threading crate
#3595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fairly straight forward and is clearly written, thanks! The most tricky thing with this is handling lifetimes but using 'static or binding to Pool are safe options.
Just a couple of small nits.
Co-authored-by: Chris Denton <chris@chrisdenton.dev>
Co-authored-by: Chris Denton <chris@chrisdenton.dev>
| pub type PTP_SIMPLE_CALLBACK = Option< | ||
| unsafe extern "system" fn(instance: PTP_CALLBACK_INSTANCE, context: *mut core::ffi::c_void), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @kennykerr, I'm once again generating new windows-rs crates against new win32metadata and noticed that upstream changed this delegate to receive zero arguments in microsoft/win32metadata@6d6617a, is that intended/correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it has two parameters as depicted here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is why I am giving you a heads-up, that this should perhaps be reported and corrected upstream. The change says (https://github.com/microsoft/win32metadata/blob/c7ffce1c97bb724580580f3856b94c6d4ad677ed/scripts/ChangesSinceLastRelease.txt#L5517-L5518):
Windows.Win32.System.Threading.PTP_SIMPLE_CALLBACK.Invoke() added
Windows.Win32.System.Threading.PTP_SIMPLE_CALLBACK.Invoke(Instance,Context) removed
This update introduces the
windows-threadingcrate as a simpler and more efficient way to handle threading on Windows. Although the Standard Library provides basic threading support, thewindows-threadingcrate offers additional features and optimizations for Windows-specific threading tasks in a manner that is generally more efficient primarily because it leverages the Windows thread pool rather than necessarily creating new threads. This is ano_stdcrate that can be used with very little overhead and only depends on thewindows-linkcrate for access to the Windows threading APIs.The existing
windows-futurecrate now depends on thewindows-threadingcrate for spawning WinRT async operations. I chose to keep this functionality separate as there are cases where the added complexity and overhead of thewindows-futurecrate may not be desirable. Thewindows-bindgencrate now also depends onwindows-threadingfor speeding up package generation that was previously disabled in #3588.The
windows-threadingcrate is intentionally simple and easy to use but offers tremendous power to scale up tasks with good data parallelism. I may add more capabilities as need arises.The
submitfunction submits the closure to the default thread pool. It is similar to the Standard Libraryspawnfunction but does not require a new thread to be created. It is a very efficient way to run some work in the background and is used by thewindows-futurecrate for implementing async operations. Waiting or joining is not currently supported but may be added in the future as a separateWorkobject.The
for_eachfunction calls the closure on each element of the iterator in parallel, waiting for all closures to finish. It is a simple way to parallelize aforloop or iterator and is used by thewindows-bindgencrate for speeding up package generation.The
for_eachfunction relies on thePoolstruct that represents a private thread pool with its own thread limits. Closures can be submitted to private pool objects rather than the default thread pool to manage concurrency or improve code isolation and thejoinmethod, orDropimplementation, will wait for all closures to run their course.Here is a preview of the crate's readme with examples.
The Windows thread pool offers many other facilities but these are especially useful and broadly applicable. For more information on the Windows thread pool see my resources here:
The Windows Thread Pool and Work
The Thread Pool Environment
Thread Pool Cancellation and Cleanup
Thread Pool Synchronization
Thread Pool Timers and I/O
The Evolution of Threads and I/O in Windows
10 Practical Techniques to Power Your Visual C++ Apps