How to create multiple threads?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BogusException

    #1

    How to create multiple threads?

    I'll ask a simple question:

    How can someone generate any number of threads from within a simple
    "for" ot Timer loop/sub (including indefinitely)?

    Dim i As Integer
    For i = 1 To 10
    Dim t As New Thread(AddressO f DoSomethingSub)
    t.IsBackground = True
    t.Start()
    Next i

    Semi-surprisingly, the above code generates only 1 thread, not 10. Now
    imagine that 100 are needed-or an indefinite amount! This approach
    simply doesn't work. In reality, the number of threads needed is
    arbitrary, so you can't just have a ton of pre-written Dim
    statements...

    So now imagine that you need to create and abort/join threads in an
    ongoing fashion. An example is doing the above in a Timer's Tick
    handler. The threads obviously need to be aborted/joined when the
    repetative task is done to free up resources or the system will simply
    run out of memory in short order.

    Sounds [deceptively] easy, doesn't it! :)

    TIA!

    BogusException

  • Patrice

    #2
    Re: How to create multiple threads?

    How do you know it creates only a single thread ? IMO you mean rather that
    you keep only the reference to the last created thread ? (in which case you
    could use an array).

    --
    Patrice

    "BogusException " <bogusexception @gmail.coma écrit dans le message de news:
    1153919480.5293 77.47360@s13g20 00...legro ups.com...
    I'll ask a simple question:
    >
    How can someone generate any number of threads from within a simple
    "for" ot Timer loop/sub (including indefinitely)?
    >
    Dim i As Integer
    For i = 1 To 10
    Dim t As New Thread(AddressO f DoSomethingSub)
    t.IsBackground = True
    t.Start()
    Next i
    >
    Semi-surprisingly, the above code generates only 1 thread, not 10. Now
    imagine that 100 are needed-or an indefinite amount! This approach
    simply doesn't work. In reality, the number of threads needed is
    arbitrary, so you can't just have a ton of pre-written Dim
    statements...
    >
    So now imagine that you need to create and abort/join threads in an
    ongoing fashion. An example is doing the above in a Timer's Tick
    handler. The threads obviously need to be aborted/joined when the
    repetative task is done to free up resources or the system will simply
    run out of memory in short order.
    >
    Sounds [deceptively] easy, doesn't it! :)
    >
    TIA!
    >
    BogusException
    >

    Comment

    • BogusException

      #3
      Re: How to create multiple threads?

      Patrice wrote:
      How do you know it creates only a single thread ? IMO you mean rather that
      you keep only the reference to the last created thread ? (in which case you
      could use an array).
      Patrice, Thanks for writing! I think you are right, but the symptom
      remains: only one thread is available to the program at a time. Your
      comment about arrays is rgeat, except that a continuously firing thread
      generator would eventually use up the memory of the computer as the
      size of the array grew!

      TIA!

      BogusException

      Comment

      • tomb

        #4
        Re: How to create multiple threads?

        Actually, the memory would be depleted only if the threads never stopped
        running. As each thread does its thing, it ends and goes away - it is
        deleted. How long do these threads actually run?

        T

        BogusException wrote:
        >Patrice wrote:
        >
        >
        >
        >>How do you know it creates only a single thread ? IMO you mean rather that
        >>you keep only the reference to the last created thread ? (in which case you
        >>could use an array).
        >>
        >>
        >
        >Patrice, Thanks for writing! I think you are right, but the symptom
        >remains: only one thread is available to the program at a time. Your
        >comment about arrays is rgeat, except that a continuously firing thread
        >generator would eventually use up the memory of the computer as the
        >size of the array grew!
        >
        >TIA!
        >
        >BogusExcepti on
        >
        >
        >

        Comment

        • Patrice

          #5
          Re: How to create multiple threads?

          Why would you want to continuously create new threads ? Also I'm not sure
          what you meant by having a single thread "available" to the program (they
          all run, if you keep a reference to each thread you can later test the state
          of any thread you created or whatever else you want).

          IMO your best bet would be to start by describing what you are trying to
          do. It looks like you have a more general problem with threads.

          Are you after a thread pool mechanism such as System.Threadin g.ThreadPool ?

          --
          Patrice

          "BogusException " <bogusexception @gmail.coma écrit dans le message de news:
          1153922235.1999 52.304300@s13g2 00...legr oups.com...
          Patrice wrote:
          >
          >How do you know it creates only a single thread ? IMO you mean rather
          >that
          >you keep only the reference to the last created thread ? (in which case
          >you
          >could use an array).
          >
          Patrice, Thanks for writing! I think you are right, but the symptom
          remains: only one thread is available to the program at a time. Your
          comment about arrays is rgeat, except that a continuously firing thread
          generator would eventually use up the memory of the computer as the
          size of the array grew!
          >
          TIA!
          >
          BogusException
          >

          Comment

          • BogusException

            #6
            Re: How to create multiple threads?

            Patrice & tomb,

            Thanks for writing!

            The objective was to generate web traffic (load) for grid testing. And
            the most efficient way I found was this:

            Public Class Request

            Public Shared iRequestsTotal As Integer ' how many simultaneous
            requests
            Public Shared iSentTotal As Integer ' how many sent total
            Public Shared iRecvdTotal As Integer ' total received
            Public Shared iTimedOutTotal As Integer ' total that timed out
            Public Shared iConcurrentRequ ests As Integer ' how many concurrent
            requests right now?

            Public Sub DoRequest(ByVal sURL As String)
            'Dim URL As String = sURL
            Try
            Dim request As WebRequest = WebRequest.Crea te(sURL)
            iSentTotal = iSentTotal + 1
            iConcurrentRequ ests += 1
            Dim response As WebResponse = request.GetResp onse()
            iRecvdTotal += 1
            response.Close( )
            iConcurrentRequ ests -= 1
            Catch ex As System.Net.WebE xception
            iTimedOutTotal += 1
            Catch ex As UriFormatExcept ion
            MsgBox(" Invalid URL:" & sURL.ToString & vbCrLf &
            ex.ToString, MsgBoxStyle.Cri tical)
            Catch ex As Exception
            MsgBox("Generic Exception:" & vbCrLf & ex.ToString,
            MsgBoxStyle.Cri tical)
            Finally
            iRequestsTotal += 1
            End Try
            End Sub

            End Class

            This class then called via:

            Sub DoURL()
            Try
            Dim r As New Request
            r.DoRequest(txt URL.Text)
            Catch ex As Exception
            MsgBox("DoURL exception: " & ex.ToString)
            End Try
            End Sub

            Which could be called either manually, or via a timer's tick:

            Private Sub tmrRequests_Tic k(ByVal sender As System.Object, ByVal e
            As System.EventArg s) Handles tmrRequests.Tic k
            If Me.bRunning = True Then
            Try
            Dim t As New Thread(New ThreadStart(Add ressOf DoURL))
            t.IsBackground = True
            t.Start()
            Catch ex As Exception
            MsgBox("tmrRequ ests_Tick exception: " & ex.ToString)
            End Try
            End If
            End Sub

            This is the whole solution, basically.

            Thanks!

            BogusException

            Comment

            • Kim Greenlee

              #7
              Re: How to create multiple threads?

              Since the problem seems to be that you are not seeing the number of threads
              you are expecting to see, I recommend that you look at a couple things.

              Set breakpoints at DoUrl()->r.DoRequest( ) and at
              tmrRequests_Tic k()->t.Start(). The reason for these two breakpoints is to
              see what is really happening. It is possible that DoUrl() returns so
              quickly that by the time you’ve finished creating all the threads that 9 of
              them have already finished. Remember, with thread debugging its all about
              timing. So by stepping through these two breakpoints you will have a chance
              to experience the context switching. This should give you an idea of whether
              the threads are truly being created. I suspect that you’ll find that the
              threads are being created as expected but that the underlying work that the
              thread is attempting is not running the way you think it should.

              After you’ve looked at the context switching, I suggest that you next drill
              down into the DoRequest() method by setting a breakpoint at the beginning of
              the method and watching what happens between each call. If you don’t see
              anything out of the ordinary, you may be slowing down execution enough that
              everything works as you expected. So remove any breakpoints and add logging
              to the method. I find that because threading bugs are so tied to timing that
              log files are a great tool for understanding exactly what is happening.
              Depending on your application you could also write to a console, but that
              will not display the results as “true” as you might need because the console
              itself gets a context switch and displays data that is in its buffer on that
              switch.

              Good luck,

              Kim Greenlee



              --
              digipede - Many legs make light work.
              Grid computing for the real world.
              Empower your business with the help of Digipede Technologies. We are a leading provider of distributed computing solutions on the Microsoft .NET platform.



              Comment

              Working...