Multi Threading Optimal Use?
-
I am using multithreading in a program, but since I have never done so before I have some questions as to optimal use. Take the following example: I have an COM object lets call it "comObj" which has a function "SendData". Since the program operates under heavy volume calling "Senddata" concurrently on seperate threads is desirable. So now my question, if I call the same function on the same object in multiple threads will I really experience a performance gain? Or do I have to create multiple objects (created inside thread) to see a performace gain? code: [code] '...in some function Dim comObj as (whatever object) 'these are passed by reference Dim mySend1 as new OutThread(comObj) Dim mySend2 as New OutThread(comObj) '...destroy objects... Public MustInherit Class ThreadWrap ' i use a threadwrapper because multiple thread subtypes Protected m_Thread As Thread Public Sub New() m_Thread = New Thread(AddressOf run) End Sub Public Sub start() Me.m_Thread.Start() End Sub Public MustOverride Sub run() End Class Public Class OutThread Inherits ThreadWrap dim m_objCom as object Public Sub New(ByRef comObj As Object) m_Thread = New Thread(AddressOf run) m_objCom = comObj End Sub Public Overrides Sub run() 'this is the sameobject used in both threads m_comObj.SendData End Sub End Class [/code] Now the alternitive I see would be to not delcare your object first and then create and destroy it inside the Run() function, but that would require twice as many calls to the constructor and destructor, plus of course more overall memory. So logically I would hope the method above does utilize multiple threads, but do all threads have to wait on that one object and does that create a bottle neck? Any clarification would be great thanks. ~rlcc
-
I am using multithreading in a program, but since I have never done so before I have some questions as to optimal use. Take the following example: I have an COM object lets call it "comObj" which has a function "SendData". Since the program operates under heavy volume calling "Senddata" concurrently on seperate threads is desirable. So now my question, if I call the same function on the same object in multiple threads will I really experience a performance gain? Or do I have to create multiple objects (created inside thread) to see a performace gain? code: [code] '...in some function Dim comObj as (whatever object) 'these are passed by reference Dim mySend1 as new OutThread(comObj) Dim mySend2 as New OutThread(comObj) '...destroy objects... Public MustInherit Class ThreadWrap ' i use a threadwrapper because multiple thread subtypes Protected m_Thread As Thread Public Sub New() m_Thread = New Thread(AddressOf run) End Sub Public Sub start() Me.m_Thread.Start() End Sub Public MustOverride Sub run() End Class Public Class OutThread Inherits ThreadWrap dim m_objCom as object Public Sub New(ByRef comObj As Object) m_Thread = New Thread(AddressOf run) m_objCom = comObj End Sub Public Overrides Sub run() 'this is the sameobject used in both threads m_comObj.SendData End Sub End Class [/code] Now the alternitive I see would be to not delcare your object first and then create and destroy it inside the Run() function, but that would require twice as many calls to the constructor and destructor, plus of course more overall memory. So logically I would hope the method above does utilize multiple threads, but do all threads have to wait on that one object and does that create a bottle neck? Any clarification would be great thanks. ~rlcc
Multiple threads will generally give you gains over single thread on these situations: 1. More responsive UI; 2. There's something the CPU could be doing while it's waiting for an I/O operation; 3. You have multiple CPUs; 4. You have a Pentium 4 with HyperThreading, on some specific situations; Notice that you won't have CPU gains, actually, as you've noticed, you will spend more CPU to do real parallel work; so, the general rule is only to MT if your CPU is waiting for something. ORACLE One Real A$#h%le Called Lary Ellison