SOLVED Threadpool in C# too slow, is there a way to speed up it? Thread.Sleep(0) and QueueUserWorkItem issues?

There really isn't a good way to tell you exactly what's making your code slow from what I see, but there are a couple of things that stand out.

There really isn't a good way to tell you exactly what's making your code slow from what I see, but there are a couple of things that stand out: Thread. Sleep(0). When you do this, you give up the rest of your timeslice from the OS, and slow down everything, because CreateDiffFrame() can't actually return until the OS scheduler comes back to it.

The object cast of Rectangle, which is a struct. You incur the overhead of boxing when this happens, which isn't going to be something you'll want for truly compute-intensive operations. Your calls to lock(this.

_state. AreaSync). It could be that AreaSync is being locked somewhere else, too, and that could be slowing things down.

You may be queueing items too granularly -- if you queue very small items of work, it's likely that the overhead of putting these items in the queue one at a time could be significant as compared to the actual amount of work done. You could also perhaps consider putting the contents of the inner loop inside the queued work item to cut down this overhead. If this is something you're trying to do for parallel computation, have you investigated using PLINQ or another such framework?

Ok thanks, 1 Maybe that's the problem 2 I'll try to see if I can use a different structure, 3 I'm sure it is not locked elsewhere, 4 Probably, I am thinking perhaps I have to use Threadpool with a bigger granularity. About PLINQ I didn't know it, now I'll take a look thanks :) – Francesco Dec 10 at 15:17.

My guess would be that it's the Sleep at the end of CreateDiffFrame. It means each thread stays alive for at least another 10 ms, if I remember correctly. You can probably do the actual work in less than 10 ms.

ThreadPool tries to optimize the usage of threads, but I think it has an upper limit to the total number of outstanding threads. So if you want to actually mimic your workload, make a tight loop that waits until the expected number of milliseconds have passed instead of a Sleep. Anyway, I don't think using ThreadPool is the actual bottleneck, using an other threading mechanism will not speed up your code.

No, Sleep(0) will just yield the CPU. It is a bad/unnecessary idea though. – Henk Holterman Dec 10 at 15:05.

There is known bug with the ThreadPool. SetMinThreads method described in KB976898: After you use the ThreadPool. SetMinThreads method in the Microsoft .

NET Framework 3.5, threads maintained by the thread pool do not work as expected You can download a fix to this behavior from here.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions