为什么不能多线程在C#中达到100%的CPU?
我工作的一个程序,处理很多要求,他们没有达到CPU的50%以上(目前我正在一个双核)。因此,我创建一个线程为每个请求,整个过程更快。处理9请求,一个线程持续02min08s,一边用3线程同时工作的时间减少到01min37s,但它一直未使用100%的CPU,只有约50%的
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 thread for each request, the whole process is faster. Processing 9 requests, a single thread lasts 02min08s, while with 3 threads working simultaneously the time decreased to 01min37s, but it keeps not using 100% CPU, only around 50%.
我怎么可以让我的程序使用完整的处理器能力?
How could I allow my program to use full processors capability?
修改 该应用程序是不是IO或内存界,他们在合理的水平所有的时间。
EDIT The application isn't IO or Memory bounded, they're at reasonable levels all the time.
我认为这事做的'双核'的事情。
I think it has something to do with the 'dual core' thing.
还有就是每一个请求使用一个锁定的方法调用,但它是非常快的,我不认为这是问题。
There is a locked method invocation that every request uses, but it is really fast, I don't think this is the problem.
我的code越多,CPU-昂贵的部分是一个DLL通过COM调用(同外部方法是从所有线程调用)。此DLL也没有记忆或IO - 界,这是一个人工智能识别组件,我做了OCR识别的薪水,薪水的要求。
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). This dll is also no Memory or IO-bounded, it is an AI recognition component, I'm doing an OCR recognition of paychecks, a paycheck for request.
EDIT2
这是非常可能的是STA COM方法是我的问题,我为了解决这个问题联系了部分业主。
It is very probable that the STA COM Method is my problem, I contacted the component owners in order to solve this problem.
你的应用程序中有显著锁定?如果线程正在等待对方很多,这很容易解释。
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...
编辑:好的,下面给出的意见,我想我们到的东西:
Okay, given the comments below, I think we're onto something:
我的code越多的CPU昂贵的部分是 通过COM一个DLL的调用(同 外部方法是从所有被称为 线程)。
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).
时的任何机会在STA运行的COM方法?如果是这样,将只使用一个线程,序列化的来电。我强烈怀疑这是关键吧。它类似于其周围的方法调用的锁(不完全一样,无可否认)。
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).