问题描述:

I have written a program in C# that does a lot of parallel work using different threads. When i reach approx 300 threads the GUI of the program starts to become slow and the execution of threads is also slowing down drastically. The threads are reading and writing data from a mySQL Database runnning on a different machine.

The funny thing is that if i split the work between two processes on the same machine everything runs perfect. Is there a thread limit per process in the .net framework or in windows? Or why am I getting this behaviour? Could it be a network related problem? I am running Windows 7 Ultimate and i have tried both VS2010 and VS 2012 with the same behaviour.

网友答案:

The way processor time is allocated is that the Operating System gives processor time to every process, then every process gives time to every thread. So two processes will get twice the processor time, and that's why it works faster if you divide the program into two processes. If you want to make the GUI run smoother, just set the priority higher for that thread. This way the GUI thread will get more processor time then the other threads, but not so much that it will noticeably slow down the other threads.

网友答案:

300 threads is silly.

The number of threads should be in the range of your number of cores (2..8) and/or the max simultaneous connections (sometimes only 4 over TCP) your system supports.

Get beyond that and you're only wasting memory, at 1 MB per thread. In a 32bit system, 300 MB is already consuming a lot of the available mem space. And I assume each thread has some buffers attached.

If 2 separate processes perform better than1 then it probably isn't the context switching but either memory usage or a connection limit that holds you back.

网友答案:

Use ThreadPool. That should automatically allocate the optimal number of threads based on your system by throttling the number of threads in existence. You can also set the maximum number of threads allowable at any one time.

Also, if you're allocating thread to parallelize tasks from within a for-loop, foreach-loop, or linq statment you should look at the Parallel Class or PLINQ.

网友答案:

The accepted answer to this question will probably explain what is happening, but 300 threads seems like to many to be a good idea for any normal application.

网友答案:

At first if you have 300 threads for an application then probably you should rethink about your program design.

Setting up GUI threads priority may give you a better performance of GUI. But if you run so much thread the OS have to allocate space in program stack. And the stack is a continuous segment of the memory. So each time you create a new thread the allocated memory space for the stack may be incapable to hold the new thread. And then the OS must have to allocate a larger continuous space in the memory and copy all the data from the old stack to new stack. So obviously this may cause performance slow of your program.

相关阅读:
Top