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
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