none
請問執行緒的啟動時機? RRS feed

  • 問題

  • 作業系統:XP pro
    RAM: 2G
    CPU: intel 雙核 Core Due T2400

    在我的程式中有一段會產生4個執行緒,並開始執行計算。
    主執行緒會等待這4個執行緒完成後,再將所有結果整合起來。
    但我監看執行緒開始的時間,似乎沒有在我下thread.Start()後,馬上開始,而是等了好幾秒。
    而且啟動後,明明cpu是雙核心的,但只有其中一個cpu跑到100%,另一個則維持在10%而以。
    請問,可能的問題發生在哪邊? 下是片段的程式碼,以及所紀錄的時間。

          StopWatch m_SW = new StopWatch();
          m_SW.Reset();
          m_SW.Start();
          //calculation
          foreach (ScanData3D stData in m_Storage.StoredData3D)
          {
            //get data from data storage
            stData.GetData(out pdZ, out pbyZ, out pbyInt, out pbyConf);
            m_LogAlgo.WriteLine("ThreadProcess : GetData {0} ms", m_SW.ElapsedMilliseconds);

            //copy data from managed to unmanaged memory
            System.Runtime.InteropServices.Marshal.Copy(pbyZ, 0, ptrXYZ, pbyZ.Length);
            System.Runtime.InteropServices.Marshal.Copy(pbyInt, 0, ptrINT, pbyZ.Length);
            m_LogAlgo.WriteLine("ThreadProcess : Marshal.Copy {0} ms", m_SW.ElapsedMilliseconds);
           
            //set data pointer
            m_bw8Height.SetImagePointer(width, height, ptrXYZ);
            m_bw8Intensity.SetImagePointer(width, height, ptrINT);
            m_LogAlgo.WriteLine("ThreadProcess : SetImagePointer {0} ms", m_SW.ElapsedMilliseconds);
           
            //產生Class ThreadProcess
            Size compensation = new Size(m_Storage.ScanInfo3D.CompensationX,      m_Storage.ScanInfo3D.CompensationY);
            ThreadProcess proc = new ThreadProcess(stData, m_bw8Height, m_bw8Intensity, Par, compensation);
            m_LogAlgo.WriteLine("ThreadProcess : new ThreadProcess {0} ms", m_SW.ElapsedMilliseconds);
           
            //產生一個Thread來計算
            Thread thread = new Thread(new ThreadStart(proc.fnThreadProc));
            thread.Priority = ThreadPriority.Highest;

            //儲存
            lstThread.Add(thread);
            lstThreadProcess.Add(proc);

          }//end of foreach loop
          m_LogAlgo.WriteLine("ThreadProcess : end loop {0} ms", m_SW.ElapsedMilliseconds);

          for (int i = 0; i < lstThread.Count; i++ )
          {
            lstThreadIdea.Start();
            m_LogAlgo.WriteLine("ThreadProcess : Thread start {0} ms", m_SW.ElapsedMilliseconds);
          }
        
     以下是這段程式碼執行時,所紀錄下來的時間。
    ThreadProcess : GetData 0 ms
    ThreadProcess : Marshal.Copy 2 ms
    ThreadProcess : SetImagePointer 2 ms
    ThreadProcess : new ThreadProcess 6 ms
    ThreadProcess : GetData 6 ms
    ThreadProcess : Marshal.Copy 7 ms
    ThreadProcess : SetImagePointer 7 ms
    ThreadProcess : new ThreadProcess 12 ms
    ThreadProcess : GetData 12 ms
    ThreadProcess : Marshal.Copy 13 ms
    ThreadProcess : SetImagePointer 13 ms
    ThreadProcess : new ThreadProcess 17 ms
    ThreadProcess : GetData 17 ms
    ThreadProcess : Marshal.Copy 19 ms
    ThreadProcess : SetImagePointer 19 ms
    ThreadProcess : new ThreadProcess 23 ms
    ThreadProcess : end loop 23 ms
    ThreadProcess : Thread start 99 ms
    ThreadProcess : Thread start 5550 ms     ==>我比較好奇的是,這邊的thread.Start()等了好幾秒才開始執行。
    ThreadProcess : Thread start 13364 ms
    ThreadProcess : Thread start 18212 ms
        
    請問有沒有先進有任何的法想,可以幫助小弟。謝謝
    2007年8月2日 上午 11:46