Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Keeping a thread idle for a while

Keeping a thread idle for a while

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sunny_vc
    wrote on last edited by
    #1

    Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:

    ThreadProcess(LPVOID)
    {
    while(TRUE)
    {
    if(flag==true) //This thread waits for the flag to be set
    {
    ExecFunc();

     }
    

    }

    }

    If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.

    Regards, Sunil Kumar

    A C R 3 Replies Last reply
    0
    • S sunny_vc

      Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:

      ThreadProcess(LPVOID)
      {
      while(TRUE)
      {
      if(flag==true) //This thread waits for the flag to be set
      {
      ExecFunc();

       }
      

      }

      }

      If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.

      Regards, Sunil Kumar

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      For waiting purpose you can use WaitForSingleObject(Just refer the usage http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx[^]) Just create a named event using CreateEvent(refer http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx[^]) Now you can wait till that event be signaled from your side. And for signaling you can use SetEvent(http://msdn.microsoft.com/en-us/library/ms686211(VS.85).aspx[^])

      Величие не Бога может быть недооценена.

      1 Reply Last reply
      0
      • S sunny_vc

        Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:

        ThreadProcess(LPVOID)
        {
        while(TRUE)
        {
        if(flag==true) //This thread waits for the flag to be set
        {
        ExecFunc();

         }
        

        }

        }

        If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.

        Regards, Sunil Kumar

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Don't use a boolean but use an event instead (and use the WaitForSingleObject[^] function). There's also a good tutorial about threads here[^]

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • S sunny_vc

          Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:

          ThreadProcess(LPVOID)
          {
          while(TRUE)
          {
          if(flag==true) //This thread waits for the flag to be set
          {
          ExecFunc();

           }
          

          }

          }

          If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.

          Regards, Sunil Kumar

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          If you expect the wait time to be relatively long, then use one of those WaitFor... functions. If the wait time would be very low, then a spin loop is an ideal candidate.

          It is a crappy thing, but it's life -^ Carlo Pallini

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups