Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista Tutorial - find number of threads waiting on a resource?

Reply
 
Old 06-16-2009   #1 (permalink)
deostroll
Guest


 
 

find number of threads waiting on a resource?

Hi,

Is there any way to know the number of threads waiting for a resource.
Suppose we have code like

lock(resource)
{
//...
}

Within the critical area I'd perform some logic that kind of
invalidates the reason other threads should execute the critical
region itself. Is this possible?

--deostroll

My System SpecsSystem Spec
Old 06-16-2009   #2 (permalink)
Jeroen Mostert
Guest


 
 

Re: find number of threads waiting on a resource?

deostroll wrote:
Quote:

> Is there any way to know the number of threads waiting for a resource.
Not in general. You'd have to implement semantics like that yourself. You
can use Semaphore to limit the amount of waiters if you don't require an
exact count, and you can use the Interlocked class for low-overhead,
thread-safe counting (which is not necessary if you already have mutual
exclusion, by the way).
Quote:

> Suppose we have code like
>
> lock(resource)
> {
> //...
> }
>
> Within the critical area I'd perform some logic that kind of
> invalidates the reason other threads should execute the critical
> region itself. Is this possible?
>
Yes, this is actually what monitors are all about (lock uses monitors under
the covers). The usual pattern goes like this:

lock (resource) {
while (!someConditionInvolvingResourceIsMet) Monitor.Wait(resource);
// now I know someConditionInvolvingResourceIsMet
// use resource, use Monitor.PulseAll() if others have to check their
conditions
}

What conditions people should wake up about and what they should do in
response is up to you, so it's possible to wake up other threads just to
tell them they shouldn't do anything. Of course, this also works without
explicitly waiting:

lock (resource) {
if (reasonForDoingThingsIsValid) {
...
}
}

...

lock (resource) {
invalidateReasonForDoingThings();
}

This works because you know only one thread is ever holding the lock. You
can therefore set flags for communicating with others at your leisure.

If you specifically want to avoid entering the lock altogether because you
don't want to block, use Monitor.TryEnter() explicitly, which allows you to
obtain a lock if and only if this can be done immediately (or within a
specified time):

if (Monitor.TryEnter(resource)) {
try {
// use resource
} finally {
Monitor.Exit(resource);
}
}

If you're avoiding the lock for performance reasons... well, that's a
chapter in itself and premature optimization in multithreading is the worst
premature optimization there is, so I'll assume that's not the case and keep
quiet for now to minimize the damage. :-)

--
J.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can't find Program Files\Windows Resource Kits\Tools in Vista General Discussion
trying to find solved threads General Discussion
how to find threads Vista performance & maintenance
find threads Vista General
Help me find a resource about Windows mail PLEASE! Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46