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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Write a function to cause a thread to wait

Write a function to cause a thread to wait

Scheduled Pinned Locked Moved C / C++ / MFC
help
7 Posts 6 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.
  • D Offline
    D Offline
    Danzy83
    wrote on last edited by
    #1

    Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.

    CPalliniC L C M L 5 Replies Last reply
    0
    • D Danzy83

      Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Dan_K wrote:

      that the CPU Usage shoots up to 100 percent

      No wonder: you're using the CPU, aren't you? That's not the best way to pause an application on a multitasking system. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • D Danzy83

        Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        You should upgrade your hardware. If you buy a dual-core system, it will become twice as efficient: the CPU load will be around 50%. Or go for a quad-core and see it sitting idle with a load of a mere 25%. If you don't like that idea, you should either use a sleep function or a timer. FWIW: you could do both to make absolutely sure. :laugh:

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        1 Reply Last reply
        0
        • D Danzy83

          Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.

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

          Your CPU usage goes to 100% simply because you are actually doing something: the while loop and the checks are operations done by the CPU, and in your case without interruption, so the CPU is always busy.

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

          CPalliniC 1 Reply Last reply
          0
          • C Cedric Moonen

            Your CPU usage goes to 100% simply because you are actually doing something: the while loop and the checks are operations done by the CPU, and in your case without interruption, so the CPU is always busy.

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

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Cedric Moonen wrote:

            so the CPU is always busy

            It's the ancient wise's rule: "keep the CPU busy and the brain sleeping..." :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • D Danzy83

              Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.

              M Offline
              M Offline
              malaugh
              wrote on last edited by
              #6

              All the CPU cycles are going to reaing the clock over and over. If you want to be independant of windows, the function uSleep is Unix the equivelent of Sleep, just add #ifndef WIN32 #define Sleep(x) uSleep(x * 1000) #endif To the top of your code

              1 Reply Last reply
              0
              • D Danzy83

                Hi, I have written a function to cause a thread to wait for a given time. But the problem is that If this function is called, I notice from task manager (in Windows) that the CPU Usage shoots up to 100 percent. But if I rather use the Windows function Sleep(), the CPU Usage falls back to around 2%, 3%, and 4%. The following is how I wrote the function, with the header file included. void Wait(clock_t milliseconds) { clock_t delay_time = milliseconds + clock(); while (delay_time > clock()) ; return; } I don't know why my CPU usage shoots up to 100% when I use this function rather than Windows Sleep() function. I don't want my application to assume the use of Windows. Please help.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Dan_K wrote:

                I don't know why my CPU usage shoots up to 100%

                Time to start learning how computers work. ;)

                It's time for a new signature.

                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