QueueUserWorkItem问题:仅创建两个工作线程,然后等待 [英] QueueUserWorkItem issues: creates only two worker threads and then waits

查看:78
本文介绍了QueueUserWorkItem问题:仅创建两个工作线程,然后等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hallo,我在Windows XP sp3中遇到QueueUserWorkItem的问题:我在现有应用程序中引入了线程池以执行一些后台工作:某些命令可能需要花费大量时间才能完成,并且我需要避免主线程锁定.

Hallo, I've got a problem with QueueUserWorkItem in Windows XP sp3: I have introduced thread pooling in an existing application to perform some background work: some commands can take a significative time to complete and I need to avoid main thread locking.

因此,每次应用程序收到命令时,它都会以这种方式使用QueueUserWorkItem:
So every time the app receives a command it uses QueueUserWorkItem in this way:

推荐答案

Hello

为Microsoft Windows编程应用程序,

如果您知道工作项可能需要很长时间才能执行,则应调用QueueUserWorkItem,并为其传递WT_EXECUTELONGFUNCTION标志.该标志帮助线程池决定是否向池中添加新线程;如果池中的所有线程都忙,它将强制线程池创建一个新线程.因此,如果您同时将10,000个工作项(带有WT_EXECUTELONGFUNCTION标志)排队,则将10,000个线程添加到线程池中.如果您不希望创建10,000个线程,则必须将对QueueUserWorkItem的调用间隔开,以便某些工作项有机会完成.

请您尝试一下WT_EXECUTELONGFUNCTION标志,让我知道是否为您工作?
Hello

As is documented in the book Programming Applications for Microsoft Windows,

If you know that a work item might take a long time to execute, you should call QueueUserWorkItem, passing it the WT_EXECUTELONGFUNCTION flag. This flag helps the thread pool decide whether to add a new thread to the pool; it forces the thread pool to create a new thread if all of the threads in the pool are busy. So if you queue 10,000 work items (with the WT_EXECUTELONGFUNCTION flag) at the same time, 10,000 threads are added to the thread pool. If you don't want 10,000 threads created, you must space out the calls to QueueUserWorkItem so that some work items get a chance to complete.

Can you please try the WT_EXECUTELONGFUNCTION flag and let me know whether it works for you?


这篇关于QueueUserWorkItem问题:仅创建两个工作线程,然后等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆