Looping with Multi Threads

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paulicus
    New Member
    • Mar 2013
    • 2

    Looping with Multi Threads

    OK so I am trying to speed a program by using mutli threading. I have used it before successfully but never in a loop. I think its pretty easy to follow my code. The problem is the 2nd time i run the thread it says "terminated or already running" ... but that makes no sense. I have tried to use a join (wait till it finishes), or abort(stop it) but neither work.

    If you curious, i am doing this because the calculation takes longer than it does to pull the data, however pulling the data does take a few seconds, so this way, i am only waitting for the AddNum function on no the pull or insert. (I am pulling data from SQL and doing some stuff and putting it back in SQL)
    Everything is defined correctly as threads, everything works perfect, until i put the loop on.
    Code:
            Do While Place <= 40100
                Prime = PrePrime
                Power = PrePower
                UpdateFactorThread.join()
                AddNumThread.Start()
    
                Place = Place + count
                count = count + 1
    
                GetInfoThread.Start()
                GetInfoThread.Join()
    
                AddNumThread.Join()
                AddNumThread.Suspend()
    
                Place = Place - 1
                UpdateFactorThread.Start()
            Loop
  • paulicus
    New Member
    • Mar 2013
    • 2

    #2
    I figured it OUT :) It was actually very easy ... So the trick is this ... create a Function that does whatever you want done. Then create an Sub That creates a thread calling that function. Now here is the trick ... you can use that same sub to call that function before the first is done. If you do you will interrupt the first thread.

    So for me i am doing calculation that take about 4 minutes, so if i simply ran a for loop the calculation have NO chance of finishing so instead, I created a public variable "Finished as Boolean" then i set it to False when the thread begins. In the main Sub i then put a loop Do While Finished is False. And in the thread set the last thing it does to Finished = True ... this mean the For loop will not continue till the thread is finished.

    now you are thinking ...what the heck is the point of multiple treads then ... :) check this out and be happy. This is how you can set up 10 threads .. Instead set Finished to an Integer ... and set the While Loop to go until Finished = 10 (for example) ... now in the thread have the last thing it does Finished = Finished +1 ... When the loop start set Finished to 0, but as each thread finishes it will increase until it hits 10. When it hits 10 the Initial loop will restart and do it all again.

    Using this method i cut a program from est. 400 day to run, down to a little over 2 days, running 15 threads. now for some basic code.
    Code:
    Public Finished as Long
    
        Public Sub AddNum1(ByVal Count As Long)
            Dim AddNumThread As New Threading.Thread(AddressOf AddNum)
            AddNumThread.Start(Count)
    
    
        End Sub
    Public Sub AddNum2(ByVal Count As Long)
            Dim AddNumThread As New Threading.Thread(AddressOf AddNum)
            AddNumThread.Start(Count)
    
        End Sub
    'Continue this pattern for however many threads you want. 
    For i = 1 to 1300000000
    
    
                Call AddNum1(i)
                Call AddNum2(i+1)
                Call AddNum3(i+2)
                Call AddNum4(i+3)
                Call AddNum5(i+4)
                Call AddNum6(5+i)
                Call AddNum7(6+i)
                Call AddNum8(7+i)
                Call AddNum9(8+i)
                Call AddNum10(9+i)
                Call AddNum11(10+i)
                Call AddNum12(11+i)
                Call AddNum13(12+i)
                Call AddNum14(13+i)
                Call AddNum15(14+i)
                Call AddNum16(15+i)
                Call AddNum17(16+i)
                Call AddNum18(17+i)
                Call AddNum19(18+i)
                Call AddNum20(19+i)
    i = i + 19 'This prevents doing the same calcs.
    Do while Finish <20
    loop
    next
    Note, the only thing you need to know about the Function AddNum, is the last thing it does is Finished = Finished +1

    Comment

    Working...