线程池执行延迟疑问

发布于 2024-09-14 00:04:22 字数 4977 浏览 5 评论 0原文

我已在循环中对 10 个委托调用 BeginInvoke。线程池不使用 10 个线程,而是仅使用两/三个线程来执行委托。有人可以解释一下原因吗?委托执行仅需几毫秒(不到 10 毫秒)。

当我在调用 BeginInvoke 之前记录线程池参数时,它表明最小线程 = 2、最大线程 = 500、可用线程 = 498。


当我调用以下托管 C++ 代码时,我遇到了问题。

void EventHelper::FireAndForget(Delegate^ d, ... array<Object^>^ args)
            {
                try
                {
                    if (d != nullptr)
                    {                                           
                        array<Delegate^>^ delegates = d->GetInvocationList();   

                        String^ message1 = String::Format("No of items in the event {0}",delegates.Length);
                        Log(LogMessageType::Information,"EventHelper.FireAndForget", message1);

                        // Iterating through the list of delegate methods.
                        for each(Delegate^ delegateMethod in delegates)
                        {
                            try
                            {
                                int minworkerThreads,maxworkerThreads,availworkerThreads, completionPortThreads;
                                ThreadPool::GetMinThreads(minworkerThreads, completionPortThreads);
                                ThreadPool::GetMaxThreads(maxworkerThreads, completionPortThreads);
                                ThreadPool::GetAvailableThreads(availworkerThreads, completionPortThreads);

                                String^ message = String::Format("FireAndForget Method {0}#{1} MinThreads - {2}, MaxThreads - {3} AvailableThreads - {4}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name, minworkerThreads, maxworkerThreads, availworkerThreads);

                                Log(LogMessageType::Information,"EventHelper.FireAndForget", message);

                                DynamicInvokeAsyncProc^ evtDelegate = gcnew DynamicInvokeAsyncProc(this, &EventHelper::OnTriggerEvent);
                                evtDelegate->BeginInvoke(delegateMethod, args, _dynamicAsyncResult, nullptr); //FIX_DEC_09 Handle Leak    
                            }
                            catch (Exception^ ex)
                            {
                                String^ message = String::Format("{0} : DynamicInvokeAsync of '{1}.{2}' failed", _id,
                                                                    delegateMethod->Method->DeclaringType, d->Method->Name);

                                Log(LogMessageType::Information,"EventHelper.FireAndForget", message);                              
                            }
                        }
                    }
                    else
                    {                   
                    }
                }
                catch (Exception^ e)
                {
                    Log(LogMessageType::Error, "EventHelper.FireAndForget", e->ToString());
                }

            }

这是委托中给出的方法

void EventHelper::OnTriggerEvent(Delegate^ delegateMethod, array<Object^>^ args)
            {
                try
                {
                    int minworkerThreads,maxworkerThreads,availworkerThreads, completionPortThreads;
                    ThreadPool::GetMinThreads(minworkerThreads, completionPortThreads);
                    ThreadPool::GetMaxThreads(maxworkerThreads, completionPortThreads);
                    ThreadPool::GetAvailableThreads(availworkerThreads, completionPortThreads);

                    String^ message = String::Format("OnTriggerEvent Method {0}#{1} MinThreads - {2}, MaxThreads - {3} AvailableThreads - {4}",
                                    delegateMethod->Method->DeclaringType, delegateMethod->Method->Name, minworkerThreads, maxworkerThreads, availworkerThreads);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);

                    message = String::Format("Before Invoke Method {0}#{1}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);

                    // Dynamically invokes (late-bound) the method represented by the current delegate. 
                    delegateMethod->DynamicInvoke(args);
                    message = String::Format("After Invoke Method {0}#{1}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);
                }
                catch (Exception^ ex)
                {
                    Log(LogMessageType::Error, "EventHelper.OnTriggerEvent", ex->ToString());
                }
            }

I have invoked BeginInvoke on 10 delegates in a loop. Instead of using 10 threads, the threadpool uses only two/three threads for executing the delegates. Can somebody please explain the reason for this?. The delegate execution takes only a few ms (less than 10ms).

When I logged threadpool parameters before invoking BeginInvoke it indicated that Min Threads = 2, Max Threads = 500, Available threads = 498.


I got the problem when I invoked the following managed c++ code.

void EventHelper::FireAndForget(Delegate^ d, ... array<Object^>^ args)
            {
                try
                {
                    if (d != nullptr)
                    {                                           
                        array<Delegate^>^ delegates = d->GetInvocationList();   

                        String^ message1 = String::Format("No of items in the event {0}",delegates.Length);
                        Log(LogMessageType::Information,"EventHelper.FireAndForget", message1);

                        // Iterating through the list of delegate methods.
                        for each(Delegate^ delegateMethod in delegates)
                        {
                            try
                            {
                                int minworkerThreads,maxworkerThreads,availworkerThreads, completionPortThreads;
                                ThreadPool::GetMinThreads(minworkerThreads, completionPortThreads);
                                ThreadPool::GetMaxThreads(maxworkerThreads, completionPortThreads);
                                ThreadPool::GetAvailableThreads(availworkerThreads, completionPortThreads);

                                String^ message = String::Format("FireAndForget Method {0}#{1} MinThreads - {2}, MaxThreads - {3} AvailableThreads - {4}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name, minworkerThreads, maxworkerThreads, availworkerThreads);

                                Log(LogMessageType::Information,"EventHelper.FireAndForget", message);

                                DynamicInvokeAsyncProc^ evtDelegate = gcnew DynamicInvokeAsyncProc(this, &EventHelper::OnTriggerEvent);
                                evtDelegate->BeginInvoke(delegateMethod, args, _dynamicAsyncResult, nullptr); //FIX_DEC_09 Handle Leak    
                            }
                            catch (Exception^ ex)
                            {
                                String^ message = String::Format("{0} : DynamicInvokeAsync of '{1}.{2}' failed", _id,
                                                                    delegateMethod->Method->DeclaringType, d->Method->Name);

                                Log(LogMessageType::Information,"EventHelper.FireAndForget", message);                              
                            }
                        }
                    }
                    else
                    {                   
                    }
                }
                catch (Exception^ e)
                {
                    Log(LogMessageType::Error, "EventHelper.FireAndForget", e->ToString());
                }

            }

This is the method given in the delegate

void EventHelper::OnTriggerEvent(Delegate^ delegateMethod, array<Object^>^ args)
            {
                try
                {
                    int minworkerThreads,maxworkerThreads,availworkerThreads, completionPortThreads;
                    ThreadPool::GetMinThreads(minworkerThreads, completionPortThreads);
                    ThreadPool::GetMaxThreads(maxworkerThreads, completionPortThreads);
                    ThreadPool::GetAvailableThreads(availworkerThreads, completionPortThreads);

                    String^ message = String::Format("OnTriggerEvent Method {0}#{1} MinThreads - {2}, MaxThreads - {3} AvailableThreads - {4}",
                                    delegateMethod->Method->DeclaringType, delegateMethod->Method->Name, minworkerThreads, maxworkerThreads, availworkerThreads);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);

                    message = String::Format("Before Invoke Method {0}#{1}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);

                    // Dynamically invokes (late-bound) the method represented by the current delegate. 
                    delegateMethod->DynamicInvoke(args);
                    message = String::Format("After Invoke Method {0}#{1}",
                                                delegateMethod->Method->DeclaringType, delegateMethod->Method->Name);
                    Log(LogMessageType::Information,"EventHelper::OnTriggerEvent", message);
                }
                catch (Exception^ ex)
                {
                    Log(LogMessageType::Error, "EventHelper.OnTriggerEvent", ex->ToString());
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲喜皆因你 2024-09-21 00:04:22

您不希望为此创建 10 个线程。最佳情况是拥有与核心一样多的活动线程。您会发现 ThreadPool.MinThreads 等于 PC 上逻辑 CPU 的数量。

将创建其他线程,但线程池故意延迟创建。 Fx4中的算法得到了改进,请参见此页面< /a>.快速浏览一下底部的图片将帮助您理解其原理。

额外的线程仅有助于补偿阻塞的线程,但这很难完全正确。

You wouldn't want 10 threads to be created for this. The optimal situation is to have as many active threads as you have cores. You'll find that ThreadPool.MinThreads equals the # of logical CPU's on your PC.

Additional Threads will be created but the ThreadPool delays that on purpose. The algorithm in Fx4 has been improved, see this page. A quick look at the picture at the bottom will help you understand the principle.

Extra threads are only helpful to compensate for blocked threads, but this is difficult to get exactly right.

杀お生予夺 2024-09-21 00:04:22

线程池在启动新线程之前故意等待一段时间 - 如果委托无论如何都快速执行(听起来确实如此),那么在几个线程上执行它们比启动新线程更有效。

来自 ThreadPool 的文档:

当所有线程池线程都已完成时
分配给任务,线程池
不立即开始创建
新的空闲线程。为了避免
不必要地分配堆栈空间
对于线程,它会创建新的空闲
间隔的线程。间隔为
目前是半秒,尽管
可能会在未来版本中改变
.NET 框架..NET 框架。

The threadpool deliberately waits a little while before starting up new threads - if the delegates execute quickly anyway (which it sounds like they do), it's more efficient to execute them on a few threads than to spin up new ones.

From the docs for ThreadPool:

When all thread pool threads have been
assigned to tasks, the thread pool
does not immediately begin creating
new idle threads. To avoid
unnecessarily allocating stack space
for threads, it creates new idle
threads at intervals. The interval is
currently half a second, although it
could change in future versions of the
.NET Framework..NET Framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文