By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

Help with first multi-thread app pls?

P: n/a
This is my first attempt at multi-thread programming, and I'm encountering a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of email
addresses. Since the actual validation can take some time, I'm spawning new
threads, up to a predetermined maximum value, to process each new address.

In my main program I have the following:

Do Until (intThreadCounter < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

intThreadCounter = intThreadCounter + 1

Finally

'Release the lock.

t = New Thread(AddressOf
oValidate.ValidateEmail)

oValidate.objEmailInfo = objEmailInfo

t.Start()

rwl.ReleaseWriterLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that locking
that wait loop would incur a deadlock. In any case, this is the only place
where I INCREMENT the intThreadCounter variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounter,
there's no chance that it will have been incremented. I start the new thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.ValidateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete(intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHandler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.ThreadComplete

...

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

If (intThreadCounter > 0) Then

intThreadCounter = intThreadCounter - 1

End If

Finally

'Release the lock.

rwl.ReleaseWriterLock()

Thread.CurrentThread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCounter = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounter is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Mar 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Joe Befumo wrote:
This is my first attempt at multi-thread programming, and I'm encountering a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of email
addresses. Since the actual validation can take some time, I'm spawning new
threads, up to a predetermined maximum value, to process each new address.

In my main program I have the following:

Do Until (intThreadCounter < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

intThreadCounter = intThreadCounter + 1

Finally

'Release the lock.

t = New Thread(AddressOf
oValidate.ValidateEmail)

oValidate.objEmailInfo = objEmailInfo

t.Start()

rwl.ReleaseWriterLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that locking
that wait loop would incur a deadlock. In any case, this is the only place
where I INCREMENT the intThreadCounter variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounter,
there's no chance that it will have been incremented. I start the new thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.ValidateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete(intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHandler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.ThreadComplete

..

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

If (intThreadCounter > 0) Then

intThreadCounter = intThreadCounter - 1

End If

Finally

'Release the lock.

rwl.ReleaseWriterLock()

Thread.CurrentThread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCounter = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounter is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


You may want to rethink how you are going about the process. Spawning a
thread is not a quick process. You may want to have one UI thread an
one worker thread that processes all your address. If you think about
how the system actually uses these threads, only one can be processing
on the processor at one time, unless you have a multi-processor system.
So the time to create the thread stack, transfer control to the new
thread takes extra time with each new thread. If you process all the
addresses in one worker thread it won't have to swap the overhead for
the thread and it will keep your system logic simpler.

Hope this helps.
Chris
Mar 12 '06 #2

P: n/a
Thanks Chris,
I believe I understand what you're saying.

Joe
"Chris" <no@spam.com> wrote in message
news:ev**************@TK2MSFTNGP10.phx.gbl...
Joe Befumo wrote:
This is my first attempt at multi-thread programming, and I'm
encountering a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of
email
addresses. Since the actual validation can take some time, I'm spawning
new
threads, up to a predetermined maximum value, to process each new
address.

In my main program I have the following:

Do Until (intThreadCounter < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

intThreadCounter = intThreadCounter + 1

Finally

'Release the lock.

t = New Thread(AddressOf
oValidate.ValidateEmail)

oValidate.objEmailInfo = objEmailInfo

t.Start()

rwl.ReleaseWriterLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that
locking
that wait loop would incur a deadlock. In any case, this is the only
place
where I INCREMENT the intThreadCounter variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounter,
there's no chance that it will have been incremented. I start the new
thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.ValidateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete(intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHandler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.ThreadComplete

..

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

If (intThreadCounter > 0) Then

intThreadCounter = intThreadCounter - 1

End If

Finally

'Release the lock.

rwl.ReleaseWriterLock()

Thread.CurrentThread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCounter = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounter is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


You may want to rethink how you are going about the process. Spawning a
thread is not a quick process. You may want to have one UI thread an one
worker thread that processes all your address. If you think about how the
system actually uses these threads, only one can be processing on the
processor at one time, unless you have a multi-processor system. So the
time to create the thread stack, transfer control to the new thread takes
extra time with each new thread. If you process all the addresses in one
worker thread it won't have to swap the overhead for the thread and it
will keep your system logic simpler.

Hope this helps.
Chris


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Mar 12 '06 #3

P: n/a
See:

Resources about asynchronous operations
http://www.mztools.com/resources_net...nousOperations

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Joe Befumo" <jo*@befumo.com> escribió en el mensaje
news:44***********************@news.newsdemon.com. ..
This is my first attempt at multi-thread programming, and I'm encountering
a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of email
addresses. Since the actual validation can take some time, I'm spawning
new
threads, up to a predetermined maximum value, to process each new address.

In my main program I have the following:

Do Until (intThreadCounter < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

intThreadCounter = intThreadCounter + 1

Finally

'Release the lock.

t = New Thread(AddressOf
oValidate.ValidateEmail)

oValidate.objEmailInfo = objEmailInfo

t.Start()

rwl.ReleaseWriterLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that
locking
that wait loop would incur a deadlock. In any case, this is the only place
where I INCREMENT the intThreadCounter variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounter,
there's no chance that it will have been incremented. I start the new
thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.ValidateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete(intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHandler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.ThreadComplete

..

'Acquire a Write lock on the resource.

rwl.AcquireWriterLock(Timeout.Infinite)

Try

If (intThreadCounter > 0) Then

intThreadCounter = intThreadCounter - 1

End If

Finally

'Release the lock.

rwl.ReleaseWriterLock()

Thread.CurrentThread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCounter = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounter is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Mar 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.