CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2006
    Posts
    13

    How to avoid resource consumption

    I am just a beginner in mutlithreading.
    I have a main program that creates a single thread, which code looks like this:

    MyThread
    {
    while (threadActive)
    {
    if (workToBeDone)
    doTheWork();
    }
    return 0;
    }

    There is no need of a mutex because the variables used in 'doTheWork()' are local, only used there. The booleans 'threadActive' and 'workToBeDone' are set my the main program: when it sets 'threadActive' to false, the thread ends, and it sets 'workToBeDone' when the 'doTheWork' function needs to be called.

    It works fine, but I have a problem:
    When the thread is active, it takes 100% of the CPU for one of the cores, even if no work needs to be done and 'workToBeDone' remains false. The 'while' loop takes all the CPU. SO I thought about adding a 'Sleep' in the loop, but the problem is that it delays the invocation of 'doTheWork' when a work needs to be done (this is a problem because it is related to real-time graphics).

    Is there a solution to avoid taking 100% of the CPU when no work has to be done and the thread just loops?

    Thank you,
    Eric

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Thumbs up Re: How to avoid resource consumption

    You could try something like this:
    Code:
      int nTime = 100;//play with this value
      while (1)
      {
         if (work)
         {
            //do your work here
         }
         else
         {
           Sleep(nTime);  //wont sleep if there is work to be done                   
         }
      }
    }
    Although this is pretty rudimentary.
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Jan 2006
    Posts
    13

    Re: How to avoid resource consumption

    This would work, but if 'work' is set to true while the thread is sleeping, there can be a delay of 100 ms (in the worst case) before the work begins. This is not acceptable because of the constraints of my application.

    Thanks for your help !!
    Eric

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to avoid resource consumption

    You typically create a manual reset event for "thread exit", and either a manual reset or auto reset event for "do work". Then the thread uses WaitForMultipleObjects() to detect when the events are in a signaled state.
    http://msdn.microsoft.com/en-us/libr...15(VS.85).aspx

    gg

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: How to avoid resource consumption

    I did say that you could change the value of the nTime variable. On Windows XP you can get about 10ms and on Vista 1ms sleeps. Those are the perspective quantums. I agree with codeplug on the waitformultipleobjects(), although you should consider how time critical your code really is.

    Quote Originally Posted by Rocky651 View Post
    This would work, but if 'work' is set to true while the thread is sleeping, there can be a delay of 100 ms (in the worst case) before the work begins. This is not acceptable because of the constraints of my application.

    Thanks for your help !!
    Eric
    ahoodin
    To keep the plot moving, that's why.

  6. #6
    Join Date
    Jan 2006
    Posts
    13

    Re: How to avoid resource consumption

    Quote Originally Posted by Codeplug View Post
    You typically create a manual reset event for "thread exit", and either a manual reset or auto reset event for "do work". Then the thread uses WaitForMultipleObjects() to detect when the events are in a signaled state.
    http://msdn.microsoft.com/en-us/libr...15(VS.85).aspx

    gg
    Indeed, using WaitForSingleObject to wait for the trigger (an event) that starts the work is much better. The CPU usage is now optimal and my thread is as fast as before !!

    Thank for your help !!
    Eric

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to avoid resource consumption

    In general event-based programming is better than busywaiting, yes. Another way to accomplish this would be to have a condition variable which the loop waits on, that gets signaled whenever it's time to move on to another task.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured