Thread handle & synchronization

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I need to get handle of thread so I can pass it into WaitHandle.WaitAny
method and wait until thread finishes its execution. How can I obtain
WaitHandle from Thread ?

Is thread signaled if it is in sleep state ? If not, is it possible to find
out when certain thread goes to sleeping state ?

Many thanks.
 
I need to get handle of thread so I can pass it into WaitHandle.WaitAny
method and wait until thread finishes its execution. How can I obtain
WaitHandle from Thread ?

I don't know that you can. A managed thread is not necessarily a
waitable OS object, and I think you need a waitable OS object to have a
WaitHandle.

That said, you can wait on a Thread instance directly. So if it's
possible to change the code so that it accepts a Thread instance
instead of a WaitHandle-derived object, that would at least allow you
to wait on the Thread. A given thread can get its own Thread reference
with the static Thread.CurrentThread property, and of course you could
also just retain the reference to the Thread when you create it.
Is thread signaled if it is in sleep state ? If not, is it possible to find
out when certain thread goes to sleeping state ?

The Thread would only be signaled when it completes. IMHO, it would be
a bad idea to design code that relies on monitoring the thread state,
but if you really want to do that you could poll the ThreadState
property of a Thread instance.

Pete
 
Hi,

Can you tell me why do you want to wait on the thread handle? The thread
handle will only signal when the thread terminates. If this is what you
wanted to know, why don't you call Thread.Join() method to wait on the
thread object until the thread terminates?

Thread handle will not signal while enterring sleep state. Also, I do not
think there is any notification for Thread.Sleep(). I would recommend you
to use AutoResetEvent for this notification/synchronization task. That is,
you may set AutoResetEvent before calling Thread.Sleep(). This will notify
other threads of the Thread.Sleep() calling. Another solution is polling
the thread state as Pete stated.

Hope this helps.

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.
 
Hi Jeffrey,

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 - I can not
modify logic in threads which will be added to thread pool.
There has to be some way how to know that thread sleeps because Windows
switch execution to new thread when active thread fall asleep ... - or this
is not possible to find out in C# ?

Thank you.
 
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
 
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.
 
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.
 
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.
 
Back
Top