Help with first multi-thread app pls?

J

Joe Befumo

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
 
C

Chris

Joe said:
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

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
 

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