7

Basically I'm working on this beat detection algorithm, the strange thing i am encountering right now is that when i split the work load on to another thread. So now i have one main thread and another worker thread. My worker thread somehow is always working faster than the main thread. It seems strange because what i have learned is that the main thread should theoretically always be faster because it is not taking time to initialise the thread. However what i get is even i pass a extra 1024 samples to the worker thread( they are both working with around 30 million samples currently) it is still faster than the main thread. Is it because i have applications running on my main thread? I'm really confused right now. here is the code

UnityEngine.Debug.Log ("T800 Start");
        Step3 s1= new Step3();
        Step3WOMT s2= new Step3WOMT();
        System.Object tempObj= samples2 as System.Object;
        float[] tempArray = new float[eS.Length/ 2];
        System.Threading.ParameterizedThreadStart parameterizedts = new System.Threading.ParameterizedThreadStart(s1.DoStep3);
        System.Threading.Thread T1 = new System.Threading.Thread(parameterizedts);
        T1.Start (tempObj);
        s2.DoStep3(samples1);
        UnityEngine.Debug.Log ("s2");
        //UnityEngine.Debug.Log (stopwatch.ElapsedMilliseconds);
        T1.Join();

Don't worry I'm only using c# features in the multithread so I believe it should be fine. What i am really confused about is that if i comment out the T1.join(); line the whole thing somehow go even slower. Im genuinely confused right now as there seems no reasonable answer to this question.

11
  • Sure, if one thread executes the same logic faster than another it is because there is less "penetration" on that thread. In WPF (.Net) f.e. The hole UI is hosted on the main thread and the application runs on that thread by default. With that knowledge it often makes sense to create multi-threaded applications to share the workload across threads in order to allow them to do more - or cosume less time for the same work. Commented Apr 22, 2015 at 5:24
  • While it is not a crazy big deal 1503 milliseconds vs 1481 milliseconds, it is just kind of strange. Also the worker thread is also doing a as object for converting the object back to float[]. So shouldn't it always be slower than the main thread? Commented Apr 22, 2015 at 5:25
  • There is really no practical difference between 1503 and 1481 ms. I would be interested to see how you are doing your timing - that probably has an impact too. Commented Apr 22, 2015 at 5:28
  • @NoelWidmer So you are saying it is just a normal scenario? will it have the same effect on iPhone 6? Basically this is a app currently designed to run on iPhone 6. I would like to know the difference so i can pace my workload better on different threads. I might have my second thread do more heavy lifting to speed up the application just a little bit more. It sucks because i only have 2 thread to really work with. Commented Apr 22, 2015 at 5:29
  • 1
    @RockyZhang - It's never "just a simple" thing to do thread timing. I'd like to see your timing code. I think it might make a difference. Commented Apr 22, 2015 at 5:43

2 Answers 2

1

T1.join() does all the magic. It allow main thread to wait till all the worker threads are complete. Is that necessary ? depends on ur application. it is expected for a main thread to wait for the end of execution of its worker threads.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Your system must have multiple cores.
  2. It's possible that Thread.Start can return not immediately after the thread is initialized. Anyway you should use ThreadPool.EnqueueUserWorkItem and ManualResetEvent to wait instead of join.
  3. To see real results your samples count must be big enough so that thread initialization time is minimal compared to the execution time of your code. ThreadPool often doesn't have to initialize a new thread but it still takes some time to launch your code. I think you should not use multithreading for tasks which takes <~50ms.
  4. If you compare the execution time for big samples count (takes few seconds) you'll see that there is no difference in the performance of the main thread and the background one (unless the main thread have higher priority).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.