Main Question:
What is the difference between QueueUserWorkItem and TrySubmitThreadpoolCallback given that they both queue a work item to the Thread Pool
TrySubmitThreadpoolCallback
This function adds a work item to the thread pool's queue (by calling PostQueuedCompletionStatus) and returns TRUE if successful; it returns FALSE if unsuccessful.
In some circumstances, such as lack of memory or quota limitations, the call to TrySubmitThreadpoolCallback might fail. Each time you call TrySubmitThreadpoolCallback, internally a work item is allocated on your behalf.
Reference: https://www.amazon.com.br/Windows-Via-C-Jeffrey-Richter/dp/0735663777
QueueUserWorkItem
Queues a method for execution where the method executes when the thread is active.
Reference: https://www.macoratti.net/09/06/vb_patp.htm
As I understand it, both functions do exactly the same thing, is the difference related to compatibility or something along these lines?
If not, what is the difference between the two?
in addition, if the answer is no, I would like that it is specified the characteristics of each one if possible.
WAIT !
If my question is incomplete, or is formatted in an inappropriate way, please comment so I can adjust it, thank you in advance, in advance.
Lucas P.
CodePudding user response:
QueueUserWorkItem
is from the legacy threadpool API. The newer function gives you more control over the environment in which the callback is executed by virtue of permitting you to initialize an environment for the callback (via InitializeThreadpoolEnvironment
). In my experience, when MS starts calling something "legacy" and I have a choice, I find it is usually better to go with the API that is not "legacy"