Processor Threads C# [closed]
How many infinte loop a core 2 due processor can handle ? im wondering...
im developing a software which i have to have 2 infinte loop inside like
do{
//code
}while(true);
i was wondering if its gonna be helpfull to insert thread.sleep(x)
inside the while body to release some pressure on the processor and if that is true how much x should be ?
You should be able to run about 2000 loops, each in its own thread, before address space of your process is exhausted. But see "If you have to ask, you're probably doing something wrong".
It depends on what you are trying to do. If you want to take 100% of your CPU time, don't insert any Sleep()
s. In other case, insert some value that you find appropriate - for example, do 10ms of work and then Sleep()
for 10ms. That way you won't kill your CPU and OS will have some leverage to determine how to schedule jobs around.
AFAIK, 10ms is the smallest schedule interval on Windows. In addition, you have option of Sleep(0)
just to inform the kernel (in this case managed thread manager) that you want others to take turn in processing.
If you just want another thread to be executed at a certain point of execution, then:
x should be 0 or 1.
But: 0 will only cause another thread to be executed if it is of equal of higher priority. 1 will force another thread to be executed.
The sleep is required unless the body of the thread is calling a blocking function (read socket for example) which is doing the sleep for you. In any other cases you need to call sleep otherwise your thread will use 100% of one cpu, eating up resources.
About how big x should be depend on what the thread is processing (how often will processing be required).
you also might try to reduce the priority of these threads so that your GUI thread (if one exists) and other applications don't freeze.
In any case modern OSs manage threads preemptively, this means that if a thread is in an infinite loop it can be paused by the OS and have another thread/process run on that core. This means that you can have any number of threads with infinite loops and the OS will take care of the scheduling. All the more so if you perform a Thread.Sleep
, this informs the OS that you want to let other threads have a go.
精彩评论