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

前端 未结 13 1327
眼角桃花
眼角桃花 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:43

    Do you have significant locking within your application? If the threads are waiting for each other a lot, that could easily explain it.

    Other than that (and the other answers given), it's very hard to guess, really. A profiler is your friend...

    EDIT: Okay, given the comments below, I think we're onto something:

    The more cpu-costly part of my code is the call of a dll via COM (the same external method is called from all threads).

    Is the COM method running in an STA by any chance? If so, it'll only use one thread, serializing calls. I strongly suspect that's the key to it. It's similar to having a lock around that method call (not quite the same, admittedly).

提交回复
热议问题