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. time-consuming thread

time-consuming thread

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
8 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.
  • R Offline
    R Offline
    RichardWdy
    wrote on last edited by
    #1

    I have a time-consuming thread about mathmatics processing. I do not want to just wait for its ending. I created a user-interface thread. CWinThread *pProcThread = AfxBeginThread(RUNTIME_CLASS(CProcThread)); After I started the thread, I found the thread almost took all CPU's time. I can't do anything, except closing it. Can anyone guide me a right way? Thanks. Windows2000 SP2, VC++ 6.0 SP5

    C 1 Reply Last reply
    0
    • R RichardWdy

      I have a time-consuming thread about mathmatics processing. I do not want to just wait for its ending. I created a user-interface thread. CWinThread *pProcThread = AfxBeginThread(RUNTIME_CLASS(CProcThread)); After I started the thread, I found the thread almost took all CPU's time. I can't do anything, except closing it. Can anyone guide me a right way? Thanks. Windows2000 SP2, VC++ 6.0 SP5

      C Offline
      C Offline
      Christopher Duncan
      wrote on last edited by
      #2

      It sounds like you've got a tight loop in the theaad somewhere, such as: while(TRUE) { // Nothing happens here to give up the cpu // spins back to top of loop } Take a look at any looping you're doing in the thread, and throw in a Sleep() call before you come back aournd to the top of the loop to give up the cup to the next thread. Sleep(0) will allow the next thread to get some action without a detrimental effect on your performance in the loop. That will probably suffice if this is your problem. If you sleep for an actual amount of milliseconds, keep in mind that in 9x, your minimum granularity (quanta) is 55 milliseconds (I may be off by a couple of milliseconds on the 9x number, been a while) and under NT it's 10 milliseconds, so even if you call Sleep(1), what you'll get will be these minimum slices depending on the platform. Chistopher Duncan Author - The Career Programmer: Guerilla Tactics for an Imperfect World (Apress)

      R 1 Reply Last reply
      0
      • C Christopher Duncan

        It sounds like you've got a tight loop in the theaad somewhere, such as: while(TRUE) { // Nothing happens here to give up the cpu // spins back to top of loop } Take a look at any looping you're doing in the thread, and throw in a Sleep() call before you come back aournd to the top of the loop to give up the cup to the next thread. Sleep(0) will allow the next thread to get some action without a detrimental effect on your performance in the loop. That will probably suffice if this is your problem. If you sleep for an actual amount of milliseconds, keep in mind that in 9x, your minimum granularity (quanta) is 55 milliseconds (I may be off by a couple of milliseconds on the 9x number, been a while) and under NT it's 10 milliseconds, so even if you call Sleep(1), what you'll get will be these minimum slices depending on the platform. Chistopher Duncan Author - The Career Programmer: Guerilla Tactics for an Imperfect World (Apress)

        R Offline
        R Offline
        RichardWdy
        wrote on last edited by
        #3

        The mathmaics process program is from a dll call. I can't put any sleep() call into it. It costs about 2-3 seconds per call(depending on the performance of the computer). My program has to call 100-400 times. What shall I do? Windows98 SE, VC++ 6.0 SP5

        J C 2 Replies Last reply
        0
        • R RichardWdy

          The mathmaics process program is from a dll call. I can't put any sleep() call into it. It costs about 2-3 seconds per call(depending on the performance of the computer). My program has to call 100-400 times. What shall I do? Windows98 SE, VC++ 6.0 SP5

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          If you want to gain some more responsiveness from your UI thread, at the cost of increasing the time taken by the worker thread, lower the priority of the latter by using SetThreadPriority. For best results, create the thread in suspended mode, set its priority and then resume it. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          1 Reply Last reply
          0
          • R RichardWdy

            The mathmaics process program is from a dll call. I can't put any sleep() call into it. It costs about 2-3 seconds per call(depending on the performance of the computer). My program has to call 100-400 times. What shall I do? Windows98 SE, VC++ 6.0 SP5

            C Offline
            C Offline
            Christopher Duncan
            wrote on last edited by
            #5

            Well, it's a little unclear from your previous post exactly what/where the problem is, so more details would be helpful. If what you're saying is that the cpu shoots to 100% utilization for 2-3 seconds each time you make a call to your math library dll (assuming it's not your code but a third party add on), then you're screwed - call their tech support and see if maybe there's something incorrect in how you're calling it. Yeah, I know, not the answer any of us ever want to hear, but if that's the case then you have to talk to the people who wrote that code. If the dll you're calling is your own code, then of course you can look for tight looping in the dll function that you're calling. If the problem is not 100% cpu utilization for 2-3 seconds every time you make that dll call, could you please be more specific about what you're experiencing? Chistopher Duncan Author - The Career Programmer: Guerilla Tactics for an Imperfect World (Apress)

            R 1 Reply Last reply
            0
            • C Christopher Duncan

              Well, it's a little unclear from your previous post exactly what/where the problem is, so more details would be helpful. If what you're saying is that the cpu shoots to 100% utilization for 2-3 seconds each time you make a call to your math library dll (assuming it's not your code but a third party add on), then you're screwed - call their tech support and see if maybe there's something incorrect in how you're calling it. Yeah, I know, not the answer any of us ever want to hear, but if that's the case then you have to talk to the people who wrote that code. If the dll you're calling is your own code, then of course you can look for tight looping in the dll function that you're calling. If the problem is not 100% cpu utilization for 2-3 seconds every time you make that dll call, could you please be more specific about what you're experiencing? Chistopher Duncan Author - The Career Programmer: Guerilla Tactics for an Imperfect World (Apress)

              R Offline
              R Offline
              RichardWdy
              wrote on last edited by
              #6

              Thank you. I have some simple questions: 1. How to change a tight loop into a loose one? My codes: for (int i = 0; i < nTimes; i++) { Mathmatics call(); // 100% CPU utilization for 2-3 seconds per call. It's from a third party. } 2. What I want to do is just lowering this thread's CPU utilization or making it run background. What shall I do? 3. Can suspended mode thread work? 4. If all of above failed, any further suggestion for me or the 3rd party?

              C T 2 Replies Last reply
              0
              • R RichardWdy

                Thank you. I have some simple questions: 1. How to change a tight loop into a loose one? My codes: for (int i = 0; i < nTimes; i++) { Mathmatics call(); // 100% CPU utilization for 2-3 seconds per call. It's from a third party. } 2. What I want to do is just lowering this thread's CPU utilization or making it run background. What shall I do? 3. Can suspended mode thread work? 4. If all of above failed, any further suggestion for me or the 3rd party?

                C Offline
                C Offline
                Christopher Duncan
                wrote on last edited by
                #7

                RichardWdy wrote: 1. How to change a tight loop into a loose one? My codes: for (int i = 0; i < nTimes; i++) { Mathmatics call(); // 100% CPU utilization for 2-3 seconds per call. It's from a third party. } Try this: for (int i = 0; i < nTimes; i++) { // you're screwed for 2-3 seconds, but... Mathmatics call(); // this will give the other threads a fighting chance Sleep(0); // if you're using overlapped i/o and want your // thread to be alertable, you'd use SleepEx(0, TRUE); } RichardWdy wrote: 2. What I want to do is just lowering this thread's CPU utilization or making it run background. What shall I do? Use AfxBeginThread instead of beginthread to kick off your thread - one of the parameters is the priority, which defaults to THREAD_PRIORITY_NORMAL. RichardWdy wrote: 3. Can suspended mode thread work? I'm not really sure what you mean by this, but you can start the thread in the paused state by using the CREATE_SUSPENDED flag, and then call ResumeThread() when you're ready to let it go. Use the thread handle in the CWinThread* that you get back in the call to ResumeThread() or any other thread API, e.g. ResumeThread(pThread->m_hThread). RichardWdy wrote: 4. If all of above failed, any further suggestion for me or the 3rd party? From the looks of that for loop, there's at least the possiblity that the problem is not in the math call. A loop like that is going to redline the cpu all on its own. Try putting that sleep in there first and see if that solves the problem. If the math call truly is causing 100% cpu utilization for 2-3 seconds, it's a tech support issue. However, if it's a small enough company that you can talk to the developers, you might suggest that they too implement some Sleep(0) calls to give up the cpu periodicially. By the way, Sleep(0) seems like it wouldn't pause at all, and it doesn't. What it does, however, is tells Windows that you're thread is ready to give up the rest of its time slice to the next waiting thread, kinda like Yield() in the old 16 bit Windows days. Hope this helps! Chistopher Duncan Author - The Career Programmer: Guerilla Tactics for an Imperfect World (Apress)

                1 Reply Last reply
                0
                • R RichardWdy

                  Thank you. I have some simple questions: 1. How to change a tight loop into a loose one? My codes: for (int i = 0; i < nTimes; i++) { Mathmatics call(); // 100% CPU utilization for 2-3 seconds per call. It's from a third party. } 2. What I want to do is just lowering this thread's CPU utilization or making it run background. What shall I do? 3. Can suspended mode thread work? 4. If all of above failed, any further suggestion for me or the 3rd party?

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  Like Chris said, just set the threads priority low. IMHO, there is little point in playing games with Sleep. Just set the priority of the thread low and let the OS do it's job. If nothing else requires CPU, the math thread will get 100% of CPU which is exactly what you want. There is no point sleeping the thread when nothing else needs the CPU. However, since the thread has a lower priority, if pratically any other thread requires the CPU, they will get it. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

                  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