none
Limit ThreadPool.SetMaxThreads to a number smaller than the number of processors RRS feed

  • Question

  • Hi,

    The documentation of ThreadPool.SetMaxThreads mention: the maximum number of worker threads or I/O completion threads cannot be set to a number smaller than the number of processors on the computer.

    Unfortunately doing so, our application will lock the CPU usage at 100% preventing other task to run smoothly.

    It is possible to set ProcessorAffinity to specify a subset of processors to use, and therefore SetMaxThreads could be set to a lower number. But this will prevent usage of a processor that may have fewer loads.

    Is it possible to achieve the same result by other mean?

    Regards,

    Huy

    Wednesday, November 23, 2016 7:49 PM

Answers

  • Dear Huy,

    >>I have a 8 core CPUs, I want to set SetMaxTreads to a lower number than 8 without setting the Processor Affinity.

    Since you cannot setting Processor Affinity, and as your previous posted, ThreadPool.SetMaxThreads mention: the maximum number of worker threads or I/O completion threads cannot be set to a number smaller than the number of processors on the computer. This is by design.

    >>Is it possible to achieve the same result by other mean?

     In conclusion It is impossible to do that.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Thursday, December 1, 2016 7:55 AM
    • Marked as answer by H_V_L Tuesday, February 14, 2017 7:38 PM
    Friday, November 25, 2016 2:06 AM

All replies

  • Consider an alternative based on Semafore [https://msdn.microsoft.com/en-us/library/system.threading.semaphore(v=vs.110).aspx]. Create such a common (static) object, then call WaitOne and also Release (see the sample in Documentation) inside the tasks that do not run well.

    Thursday, November 24, 2016 12:34 PM
  • Hi Huy,

    Do you mean you want to restrict your task running on the specified CPU core, but once other CPU core is free, you want the task can also running on them? Sorry, I am not good at about CPU core.But I hope the following will give you some clue. 

    In my opinion, the nearest solution is avoid setting thread affinity and let system determine which core to use. One of the design purpose of thread pool is to take full advantage of muti-core CPU.
    For the scenario you want to implement ,  I found  the Wiki mentioned a few words. https://en.wikipedia.org/wiki/Processor_affinity
    "Scheduling-algorithm implementations vary in adherence to processor affinity. Under certain circumstances, some implementations will allow a task to change to another processor if it results in higher efficiency. "

    As the operation of dispatch the core to thread is kernel level, it depend on the Scheduling-algorithm which can not  be easily controled or maybe could not be controlled.  I've not  find any document about that in such a short time. If you indeed want to understand. I suggest research on windows kernel to learn more about Scheduling-algorithm. That is a possiable choice worth trying.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Kristin Xie Thursday, November 24, 2016 2:14 PM
    Thursday, November 24, 2016 2:13 PM
  • I have a 8 core CPUs, I want to set SetMaxTreads to a lower number than 8 without setting the Processor Affinity.


    • Edited by H_V_L Thursday, November 24, 2016 5:09 PM
    Thursday, November 24, 2016 5:09 PM
  • Dear Huy,

    >>I have a 8 core CPUs, I want to set SetMaxTreads to a lower number than 8 without setting the Processor Affinity.

    Since you cannot setting Processor Affinity, and as your previous posted, ThreadPool.SetMaxThreads mention: the maximum number of worker threads or I/O completion threads cannot be set to a number smaller than the number of processors on the computer. This is by design.

    >>Is it possible to achieve the same result by other mean?

     In conclusion It is impossible to do that.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Kristin Xie Thursday, December 1, 2016 7:55 AM
    • Marked as answer by H_V_L Tuesday, February 14, 2017 7:38 PM
    Friday, November 25, 2016 2:06 AM
  • If your thread is a long thread(will run long time), you can use Thread instead of ThreadPool. If your thread is short thread, I think there is no problem that you set the ThreadPool size to bigger number.

    Anyway, if you want to limit the thread count for some other reason(access the resource etc), you can use Thread with some control code for thread count.

    Thanks

    Friday, November 25, 2016 2:33 AM
  • It would be a nice enhancement, especially as the number of core increase in a CPU, an application may want to have a finer control.
    Tuesday, February 14, 2017 7:40 PM
  • If you goal is to simply throttle the CPU so that it doesn't consume all the processor time, then one option might be to add the process to a JobObject with a cpu rate limit.


    Tuesday, February 14, 2017 9:16 PM