Home All Groups Group Topic Archive Search About

Help with first multi-thread app pls?

Author
12 Mar 2006 8:12 PM
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




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

Author
12 Mar 2006 8:51 PM
Chris
Joe Befumo wrote:
Show quoteHide quote
> 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
Author
12 Mar 2006 10:28 PM
Joe Befumo
Thanks Chris,
    I believe I understand what you're saying.

Joe


Show quoteHide quote
"Chris" <no@spam.com> wrote in message
news:ev0fMbhRGHA.2532@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
Author
13 Mar 2006 2:27 PM
Carlos J. Quintero [VB MVP]
See:

Resources about asynchronous operations
http://www.mztools.com/resources_net_developers.htm#AsynchronousOperations

--

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


Show quoteHide quote
"Joe Befumo" <j**@befumo.com> escribió en el mensaje
news:441480a0$0$18143$b9f67a60@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