简体   繁体   English

C#multithreading奇怪的行为

[英]C# multithreading Strange behaviour

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. 然而,我得到的是,即使我将额外的1024个样本传递给工作线程(它们目前都在使用大约3000万个样本),它仍然比主线程更快。 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. 别担心我只在多线程中使用c#功能,所以我相信它应该没问题。 What i am really confused about is that if i comment out the T1.join(); 我真的很困惑的是,如果我注释掉T1.join(); line the whole thing somehow go even slower. 把整个事情排成一线甚至更慢。 Im genuinely confused right now as there seems no reasonable answer to this question. 我现在真的很困惑,因为这个问题似乎没有合理的答案。

T1.join() does all the magic. T1.join()完成了所有的魔法。 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. 期望主线程等待其工作线程的执行结束。

  1. Your system must have multiple cores. 您的系统必须具有多个核心。
  2. It's possible that Thread.Start can return not immediately after the thread is initialized. 线程初始化后, Thread.Start可能不会立即返回。 Anyway you should use ThreadPool.EnqueueUserWorkItem and ManualResetEvent to wait instead of join. 无论如何,你应该使用ThreadPool.EnqueueUserWorkItemManualResetEvent来等待而不是加入。
  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. ThreadPool通常不必初始化新线程,但仍需要一些时间来启动代码。 I think you should not use multithreading for tasks which takes <~50ms. 我认为你不应该使用多线程来完成<~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). 如果比较大样本计数的执行时间(需要几秒钟),您将看到主线程和后台线程的性能没有差异(除非主线程具有更高的优先级)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM