none
Multi Thread not responding RRS feed

  • Question

  •  I'm testing a multi thread applications. 

    But when i set max thread for threadpool is 3000, at runtime, ThreadPool only have 1400 thread. (CPU at the moment is 10% and RAM about 100Mb)

    And after about 5s, application is not responding. Please help me!

    This is my code

    using (ManualResetEvent mre = new ManualResetEvent(false))
                {
                    for (int i = 0; i < 3000; i++)
                    {
                        this.Invoke(new System.Action(delegate()
                        {
                            ThreadPool.QueueUserWorkItem(delegate
                            {                            
                                lock (_lock)
                                {
                                    Interlocked.Increment(ref iCountThread);
                                    Logs.appendLog2(iCountThread.ToString());
                                }
                                //exec3();
                            });
                        }));
                    }
                    mre.WaitOne();
                }


    • Edited by LuuQuangICT Tuesday, October 10, 2017 12:09 PM
    Tuesday, October 10, 2017 12:08 PM

Answers

  • Have you heard the saying too many cooks in the kitchen or too many cooks spoil the broth? The philosophy can apply to programmers too; adding more programmers to a project could make the project take more time, not less time. Too many threads can make things worse.

    If the processor and memory are not getting used much then the bottleneck is whatever they are waiting on. You need to determine what that is and optimize that.



    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by cheong00Editor Wednesday, October 11, 2017 1:22 AM
    • Marked as answer by cheong00Editor Wednesday, October 18, 2017 1:43 AM
    Tuesday, October 10, 2017 8:23 PM
  • The current threadpool implementation ultimately relies on physical threads. In Windows, there is a limit on how many threads you can create in a single process. For an x86 app that is approximately 2K. For x64 the theoretical limit is higher but as discussed in this blog post, the actual limit may be a lot lower.

    The net take away is that you should never try to create that many threads in a single process. Adding more threads to your test does not in any way make it more tested. You could set the value to 200 and have the exact same test scenarios. If you want to test what happens when you cannot allocate a new thread then test that scenario explicitly, don't try to run out of threads manually. It is unreliable.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by cheong00Editor Wednesday, October 11, 2017 1:22 AM
    • Marked as answer by cheong00Editor Wednesday, October 18, 2017 1:43 AM
    Tuesday, October 10, 2017 4:29 PM
    Moderator

  • Hi LuuQuangICT,

    I have tried to keep track of the number of threads yet to finish. When that variable reaches zero, we signal on one specific ManualResetEvent object that all threads are done.

          string _lock = "lock";
            private int iCountThread = 3000;
            private void button1_Click(object sender, EventArgs e)
            {
                using (ManualResetEvent mre = new ManualResetEvent(false))
                {
                    for (int i = 0; i < 3000; i++)
                    {
                        this.Invoke(new System.Action(delegate ()
                        {
                            ThreadPool.QueueUserWorkItem(delegate
                            {
                                lock (_lock)
                                {                            
                                    Console.WriteLine("Thread number: {0}", i);
                                    
                                    if (Interlocked.Decrement(ref iCountThread) == 0)
                                    {
                                        mre.Set();
                                    }
    
                                }
                            });
                        }));
                    }
                    mre.WaitOne();
                }
            }


    AutoResetEvent and ManualResetEvent in C#:
    http://www.c-sharpcorner.com/UploadFile/ff0d0f/autoresetevent-and-manualresetevent-in-C-Sharp/

    How to: Use a Thread Pool (C# Programming Guide):
    https://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80).aspx

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Yohann Lu



    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.


    Wednesday, October 11, 2017 6:28 AM
    Moderator

All replies

  • The current threadpool implementation ultimately relies on physical threads. In Windows, there is a limit on how many threads you can create in a single process. For an x86 app that is approximately 2K. For x64 the theoretical limit is higher but as discussed in this blog post, the actual limit may be a lot lower.

    The net take away is that you should never try to create that many threads in a single process. Adding more threads to your test does not in any way make it more tested. You could set the value to 200 and have the exact same test scenarios. If you want to test what happens when you cannot allocate a new thread then test that scenario explicitly, don't try to run out of threads manually. It is unreliable.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by cheong00Editor Wednesday, October 11, 2017 1:22 AM
    • Marked as answer by cheong00Editor Wednesday, October 18, 2017 1:43 AM
    Tuesday, October 10, 2017 4:29 PM
    Moderator
  • Have you heard the saying too many cooks in the kitchen or too many cooks spoil the broth? The philosophy can apply to programmers too; adding more programmers to a project could make the project take more time, not less time. Too many threads can make things worse.

    If the processor and memory are not getting used much then the bottleneck is whatever they are waiting on. You need to determine what that is and optimize that.



    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by cheong00Editor Wednesday, October 11, 2017 1:22 AM
    • Marked as answer by cheong00Editor Wednesday, October 18, 2017 1:43 AM
    Tuesday, October 10, 2017 8:23 PM

  • Hi LuuQuangICT,

    I have tried to keep track of the number of threads yet to finish. When that variable reaches zero, we signal on one specific ManualResetEvent object that all threads are done.

          string _lock = "lock";
            private int iCountThread = 3000;
            private void button1_Click(object sender, EventArgs e)
            {
                using (ManualResetEvent mre = new ManualResetEvent(false))
                {
                    for (int i = 0; i < 3000; i++)
                    {
                        this.Invoke(new System.Action(delegate ()
                        {
                            ThreadPool.QueueUserWorkItem(delegate
                            {
                                lock (_lock)
                                {                            
                                    Console.WriteLine("Thread number: {0}", i);
                                    
                                    if (Interlocked.Decrement(ref iCountThread) == 0)
                                    {
                                        mre.Set();
                                    }
    
                                }
                            });
                        }));
                    }
                    mre.WaitOne();
                }
            }


    AutoResetEvent and ManualResetEvent in C#:
    http://www.c-sharpcorner.com/UploadFile/ff0d0f/autoresetevent-and-manualresetevent-in-C-Sharp/

    How to: Use a Thread Pool (C# Programming Guide):
    https://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80).aspx

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Yohann Lu



    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.


    Wednesday, October 11, 2017 6:28 AM
    Moderator