Counting threads

C

cj

I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and starts a
thread to handle each requested connection. These are short lived
threads that get a request and return a reply and end.

Can the main thread tell me how many connection threads are currently
running at any given time? I'd like to have a label on the main form to
show how many connections the server is currently servicing. Maybe I'd
put a timer on the form and every 5 seconds or so update the count.
 
S

Shawn

I do something similar with one of my programs.

What I do is add to a global collection when a thread is started and
remove from the collection when the thread ends. This why I can just
query the count of the collection to see how many threads are currently
being serviced.
 
H

Herfried K. Wagner [MVP]

cj said:
I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and starts a
thread to handle each requested connection. These are short lived threads
that get a request and return a reply and end.

In addition to the other reply you may want to take a look at the
'ThreadPool' class.
 
S

Stephany Young

I think it's a bit simpler than the others indicated.

If you create a small class thus:

Public Class MyThreadCount

Private Shared m_lock As New Object

Private Shared m_threadcount As Int32 = 0

Public Shared Sub Increment()

Synclock(m_lock)
m_threadcount += 1
End Synclock

End Sub

Public Shared Sub Decrement()

Synclock(m_lock)
m_threadcount -= 1
End Synclock

End Sub

Public Shared Sub Reset()

Synclock(m_lock)
m_threadcount = 0
End Synclock

End Sub

Public Shared ReadOnly Property ThreadCount() As Int32

Get
Dim _count as Int32
Synclock(m_lock)
_count= m_threadcount
End Synclock
Return _count
End Get

End Property

End Class

In each of your connection handling threads make a call to
MyThreadCount.Increment as the first statement in the thread and a call to
MyThreadCount.Decrement as the last call in the thread.

Sub ConnectionHandlerThread()

MyThreadCount.Increment

Try
 
C

cj

I understand your code but I do have one question. As written couldn't
one thread be incrementing m_threadcount at the very instant that
another is decrementing it? Wouldn't this be a problem too? If so I'd
think it'd need to be one sub that changes the value of m_threadcount
and the operator be passed as a variable to it. What do you think?
 
S

Stephany Young

I covered that in the paragraph that starts with 'The use of Synclock'.

If it will make it easier to understand, then take out all the 'Shared'
keywords, and instantiate 1 (and 1 only) instance of type MyThreadCount as a
public variable:

Public MyThreadCount As New MyThreadCount

Thus, is can be seen that all threads are using the same instance.

When one thread calls MyThreadCount.Increment, it acquires a lock on the
m_lock object. Any calls by other threads before that lock is released will
queue up.

When the first thread releases the lock on the m_lock object, the next
thread in the queue will acquire the lock and so on ad infinitum.

Yes, it is true that waiting for a lock will block the calling thread but we
are only talking about nanoseconds.
 
C

cj

Ok, I did read your post and understood that synclock would allow one
thread at a time but was unclear as to whether that meant one thread at
a time across all subs. I see now it does. Thanks
 
?

=?Windows-1252?Q?Jos=E9_Manuel_Ag=FCero?=

Hi Stephany,

Your code is a good general purpose sample, but for this particular matter you don't need synclock.
As the variable you are modifying is an integer, you can use the interlocked class to increment and decrement the variable anywhere without acquiring and releasing locks.
As the access to an integer is an atomic operation, you can safely read the value at any moment.

Regards.


"Stephany Young" <noone@localhost> escribió en el mensaje |I think it's a bit simpler than the others indicated.
|
| If you create a small class thus:
|
| Public Class MyThreadCount
|
| Private Shared m_lock As New Object
|
| Private Shared m_threadcount As Int32 = 0
|
| Public Shared Sub Increment()
|
| Synclock(m_lock)
| m_threadcount += 1
| End Synclock
|
| End Sub
|
| Public Shared Sub Decrement()
|
| Synclock(m_lock)
| m_threadcount -= 1
| End Synclock
|
| End Sub
|
| Public Shared Sub Reset()
|
| Synclock(m_lock)
| m_threadcount = 0
| End Synclock
|
| End Sub
|
| Public Shared ReadOnly Property ThreadCount() As Int32
|
| Get
| Dim _count as Int32
| Synclock(m_lock)
| _count= m_threadcount
| End Synclock
| Return _count
| End Get
|
| End Property
|
| End Class
|
| In each of your connection handling threads make a call to
| MyThreadCount.Increment as the first statement in the thread and a call to
| MyThreadCount.Decrement as the last call in the thread.
|
| Sub ConnectionHandlerThread()
|
| MyThreadCount.Increment
|
| Try
| .
| .
| .
| Catch ...
| .
| .
| .
| Finally
| ' Any other cleanup code
| MyThreadCount.Decrement
| End Try
|
| End Sub
|
| Putting the thread code in a Try...Catch...Finally and having the call to
| MyThreadCount.Decrement as the last line in the Finally section ensures that
| the thread cannot terminate without calling MyThreadCount.Decrement.
|
| When you want to know how many threads are active simply interrogate the
| value of the MyThreadCount.ThreadCount property.
|
| The use of Synclock ensures that only 1 thread at a time can update the
| counter and that no thread can update the counter while it is being
| interrogated.
|
| Note that the value of the ThreadCount property represents the number of
| active threads at the point in time thate the _count= m_threadcount
| statement is executed.
|
| By the time you see the result displayed more threads may have started
| and/or finished.
|
| Because it is likely to be useful, I have included a Reset method which is
| self explanatory.
|
| I am sure that you will be able to tailor the example to suit you own needs
| and make it more robust.
|
|
| | > I'm writing a TCP/IP server app that will have many simultaneous
| > connections. The main thread listens for new connections and starts a
| > thread to handle each requested connection. These are short lived threads
| > that get a request and return a reply and end.
| >
| > Can the main thread tell me how many connection threads are currently
| > running at any given time? I'd like to have a label on the main form to
| > show how many connections the server is currently servicing. Maybe I'd
| > put a timer on the form and every 5 seconds or so update the count.
 
T

TerryFei

Hi cj,
Thanks for Stephany's reply!

I just wanted to check how things are going . If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
Date: Thu, 16 Feb 2006 08:24:55 -0500
From: cj <[email protected]>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <[email protected]>
<[email protected]>
 
C

cj

The project is going fine. I'm using Stephany Young's code.
Hi cj,
Thanks for Stephany's reply!

I just wanted to check how things are going . If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
Date: Thu, 16 Feb 2006 08:24:55 -0500
From: cj <[email protected]>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <[email protected]>
<[email protected]>
In-Reply-To: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:317979
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Ok, I did read your post and understood that synclock would allow one
thread at a time but was unclear as to whether that meant one thread at
a time across all subs. I see now it does. Thanks
 
T

TerryFei

Hi cj,
I am glad to know that the problem is resolved now. Thanks for
participating the community!

Best Regards,

Terry Fei [MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security

--------------------
Date: Mon, 20 Feb 2006 08:04:08 -0500
From: cj <[email protected]>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
In-Reply-To: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:318522
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

The project is going fine. I'm using Stephany Young's code.
Hi cj,
Thanks for Stephany's reply!

I just wanted to check how things are going . If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
Date: Thu, 16 Feb 2006 08:24:55 -0500
From: cj <[email protected]>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Subject: Re: Counting threads
References: <[email protected]>
<[email protected]>
In-Reply-To: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.vb
NNTP-Posting-Host: 208.254.170.98
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:317979
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Ok, I did read your post and understood that synclock would allow one
thread at a time but was unclear as to whether that meant one thread at
a time across all subs. I see now it does. Thanks


Stephany Young wrote:
I covered that in the paragraph that starts with 'The use of Synclock'.

If it will make it easier to understand, then take out all the 'Shared'
keywords, and instantiate 1 (and 1 only) instance of type
MyThreadCount
as a
public variable:

Public MyThreadCount As New MyThreadCount

Thus, is can be seen that all threads are using the same instance.

When one thread calls MyThreadCount.Increment, it acquires a lock on the
m_lock object. Any calls by other threads before that lock is released will
queue up.

When the first thread releases the lock on the m_lock object, the next
thread in the queue will acquire the lock and so on ad infinitum.

Yes, it is true that waiting for a lock will block the calling thread but we
are only talking about nanoseconds.


I understand your code but I do have one question. As written
couldn't
one
thread be incrementing m_threadcount at the very instant that another is
decrementing it? Wouldn't this be a problem too? If so I'd think it'd
need to be one sub that changes the value of m_threadcount and the operator
be passed as a variable to it. What do you think?


Stephany Young wrote:
I think it's a bit simpler than the others indicated.

If you create a small class thus:

Public Class MyThreadCount

Private Shared m_lock As New Object

Private Shared m_threadcount As Int32 = 0

Public Shared Sub Increment()

Synclock(m_lock)
m_threadcount += 1
End Synclock

End Sub

Public Shared Sub Decrement()

Synclock(m_lock)
m_threadcount -= 1
End Synclock

End Sub

Public Shared Sub Reset()

Synclock(m_lock)
m_threadcount = 0
End Synclock

End Sub

Public Shared ReadOnly Property ThreadCount() As Int32

Get
Dim _count as Int32
Synclock(m_lock)
_count= m_threadcount
End Synclock
Return _count
End Get

End Property

End Class

In each of your connection handling threads make a call to
MyThreadCount.Increment as the first statement in the thread and a call
to MyThreadCount.Decrement as the last call in the thread.

Sub ConnectionHandlerThread()

MyThreadCount.Increment

Try
.
.
.
Catch ...
.
.
.
Finally
' Any other cleanup code
MyThreadCount.Decrement
End Try

End Sub

Putting the thread code in a Try...Catch...Finally and having the
call
to
MyThreadCount.Decrement as the last line in the Finally section ensures
that the thread cannot terminate without calling MyThreadCount.Decrement.
When you want to know how many threads are active simply interrogate the
value of the MyThreadCount.ThreadCount property.

The use of Synclock ensures that only 1 thread at a time can update the
counter and that no thread can update the counter while it is being
interrogated.

Note that the value of the ThreadCount property represents the
number
of
active threads at the point in time thate the _count= m_threadcount
statement is executed.

By the time you see the result displayed more threads may have started
and/or finished.

Because it is likely to be useful, I have included a Reset method which
is self explanatory.

I am sure that you will be able to tailor the example to suit you own
needs and make it more robust.


I'm writing a TCP/IP server app that will have many simultaneous
connections. The main thread listens for new connections and
starts
a
thread to handle each requested connection. These are short lived
threads that get a request and return a reply and end.

Can the main thread tell me how many connection threads are currently
running at any given time? I'd like to have a label on the main
form
to
show how many connections the server is currently servicing. Maybe I'd
put a timer on the form and every 5 seconds or so update the count.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top