|
From: John on 6 Nov 2007 14:27 Thank you for the answer. It should be O.K. for me to have a more active threads than is a maximal number of allowed threads - same as it is O.K. for completion ports. What I was asking is purely possibility how deep can I go while implementing thread pool. I did thread pool which manages methods to execute but I wanted to know if it is possible to do thread pool which manages independent threads (as opposite to methods). I know that thread takes method as its start point but simply I wanted to know if it is possible to manage threads in some way. Thanks. "Peter Duniho" wrote: > On 2007-11-05 09:05:05 -0800, John <aacdio(a)nospam.nospam> said: > > > I want to creat custom thread pool so I can start next thread when some > > active thread from pool goes sleep or finishes its execution > > The latter (finishes its execution) I understand (but the existing > thread pools already deal with this just fine), but not the former > (sleeps). > > The OS does a very nice job of scheduling threads, allowing one to run > when another is sleeping. Furthermore, what are you going to do if a > thread sleeps, then while a new thread you've started continues > working, the original thread wakes back up? If it's okay for the > second thread to continue working while the original thread is now not > sleeping, why do you have to wait for the original thread to sleep in > the first place? > > If you really need multiple threads to coordinate when they are > executing, the code to manage that needs to exist from within the > threads. The threads can use synchronization objects, like an > AutoResetEvent, to coordinate execution, setting another thread's event > just before waiting on its own. > > As I said in a different thread recently, only the threads themselves > have sufficient knowledge to correctly manage when they are executing > and not executing. You'd just be asking for big trouble to try to > manage that state of the threads externally. > > There are a number of reasons for this, but one of the biggest is that > that's what the Windows thread scheduler is _already doing_. Trying to > add your own layer on top of that is bound to create some sort of > conflict somewhere. At best, you'll kill your application's > performance, and at worst you could create all sorts of hard-to-debug > bugs. > > > - I can not > > modify logic in threads which will be added to thread pool. > > The usual way a thread pool works is that the threads always exist, and > the pull some sort of execution context from a queue (for example, a > delegate to be executed) as they complete previous execution contexts. > No need to modify the logic within the code being executed by the > thread pool, since all of the control logic is outside of that code. > > Of course, as I mentioned, the built-in thread pools already handle > this behavior perfectly. > > > There has to be some way how to know that thread sleeps because Windows > > switch execution to new thread when active thread fall asleep ... > > The Windows thread scheduler operates at a much lower level. > Furthermore, it is inherently tied to the act of "sleeping". A thread > cannot sleep without the participation of the thread scheduler, so it's > trivial for the thread scheduler to detect this case and switch to a > different thread. > > But this behavior is not really exposed in any useful way outside of > the thread scheduler. > > > - or this > > is not possible to find out in C# ? > > It's not really even possible in a practical way with the native Win32 API. > > That said, I doubt you really need to do what you think you need to do. > I think it's likely that if you stepped back and concisely described > the higher-level goal you're trying to achieve, some advice could be > provided for implementing that higher-level goal without doing the sort > of thread manipulation you say you want to do. > > Pete > >
From: "Jeffrey Tan[MSFT]" on 6 Nov 2007 22:10 Hi, Sorry for the late response, I am out of office yesterday. Based on my knowledge, one key points to implement a smart threadpool is the scalability. That is the thread number in the threadpool should be able to increase or decrease based on the CPU number, busy threads number, free threads number etc... So we need some dynamic mechanism to check and manage the threads in the pool. As I know, the best mechanism to manage thread pool is using I/O completion port. This is because I/O completion port is a Windows OS built-in mechanism, also it has several cool internal queues which schedule free threads based on the busy or I/O threads number. Also, I/O completion port provides a strong support for the inter-thread communication. Using I/O completion port, there is no need to call Sleep() to make threads sleep in the pool, we are calling GetQueuedCompletionStatus() to make it sleep. The chapter 2 "Device I/O and Interthread Communication" in Jeffrey Richter's book <Programming Server-Side Applications for Microsoft Windows 2000> talks about I/O completion port and provides some good guideline regarding implement threadpool. The weak point is that the I/O completion port is completely a Win32 OS feature instead of .Net mechanism. You may refer to the article below regarding implementing a smart thread pool: "Smart Thread Pool" http://www.codeproject.com/cs/threads/smartthreadpool.asp Thanks. Best regards, Jeffrey Tan Microsoft Online Community Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
From: "Jeffrey Tan[MSFT]" on 8 Nov 2007 22:21 Hi, Have you reviewed my last reply to you? Does it make sense to you? If you still need any help, please feel free to feedback, thanks. Best regards, Jeffrey Tan Microsoft Online Community Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
|
Pages: 1 Prev: Problem uploading a file with HttpWebRequest Next: Upgrade from 2.0 to 3.0 |