Why doesn't multithreading in C# reach 100% CPU?

前端 未结 13 1357
眼角桃花
眼角桃花 2021-01-30 22:17

I\'m working on a program that processes many requests, none of them reaching more than 50% of CPU (currently I\'m working on a dual core). So I created a threa

13条回答
  •  感情败类
    2021-01-30 22:32

    It depends what your program does - the work carried out by your concurrent Requests could be IO-bound - limited by the speed of (eg) your hard disk - rather than CPU bound, when you would see your CPU hit 100%.

    After the edit, it does sound like COM STA objects might be the culprit.

    Do all threads call the same instance of the COM object? Would it be possible to make your worker thread STA threads, and create a separate instance of the COM object on each thread. In this way it might be possible to avoid the STA bottleneck.

    To tell if a COM coclass is STA:

    class Test
    {
      static void Main() //This will be an MTA thread by default
      {
        var o = new COMObjectClass();
        // Did a new thread pop into existence when that line was executed?
        // If so, .NET created an STA thread for it to live in.
      }
    }
    

提交回复
热议问题