Click here to Skip to main content
16,010,394 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralActiveX Pin
syedhassan423-Mar-02 5:56
syedhassan423-Mar-02 5:56 
Generaltime-consuming thread Pin
RichardWdy23-Mar-02 4:28
RichardWdy23-Mar-02 4:28 
GeneralRe: time-consuming thread Pin
Christopher Duncan23-Mar-02 4:41
Christopher Duncan23-Mar-02 4:41 
GeneralRe: time-consuming thread Pin
RichardWdy23-Mar-02 4:52
RichardWdy23-Mar-02 4:52 
GeneralRe: time-consuming thread Pin
Joaquín M López Muñoz23-Mar-02 4:55
Joaquín M López Muñoz23-Mar-02 4:55 
GeneralRe: time-consuming thread Pin
Christopher Duncan23-Mar-02 4:58
Christopher Duncan23-Mar-02 4:58 
GeneralRe: time-consuming thread Pin
RichardWdy24-Mar-02 0:51
RichardWdy24-Mar-02 0:51 
GeneralRe: time-consuming thread Pin
Christopher Duncan24-Mar-02 6:37
Christopher Duncan24-Mar-02 6:37 
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)
GeneralRe: time-consuming thread Pin
Tim Smith24-Mar-02 6:47
Tim Smith24-Mar-02 6:47 
GeneralDLL Pin
Stan the man23-Mar-02 4:10
Stan the man23-Mar-02 4:10 
GeneralRe: DLL Pin
Joaquín M López Muñoz23-Mar-02 4:50
Joaquín M López Muñoz23-Mar-02 4:50 
QuestionHow to know if my IP is accessible from outside? Pin
Rickard Andersson2023-Mar-02 3:07
Rickard Andersson2023-Mar-02 3:07 
AnswerRe: How to know if my IP is accessible from outside? Pin
Nish Nishant23-Mar-02 3:26
sitebuilderNish Nishant23-Mar-02 3:26 
AnswerRe: How to know if my IP is accessible from outside? Pin
Josh Knox (disabled)23-Mar-02 4:03
Josh Knox (disabled)23-Mar-02 4:03 
AnswerRe: How to know if my IP is accessible from outside? Pin
Anders Molin23-Mar-02 4:05
professionalAnders Molin23-Mar-02 4:05 
QuestionWhat are callback functions Pin
23-Mar-02 2:35
suss23-Mar-02 2:35 
AnswerRe: What are callback functions Pin
Nish Nishant23-Mar-02 2:58
sitebuilderNish Nishant23-Mar-02 2:58 
Generalright to left layout Pin
Drawil23-Mar-02 0:46
Drawil23-Mar-02 0:46 
GeneralRe: right to left layout Pin
moliate23-Mar-02 1:06
moliate23-Mar-02 1:06 
GeneralRe: right to left layout Pin
Drawil23-Mar-02 1:23
Drawil23-Mar-02 1:23 
GeneralRe: right to left layout Pin
moliate23-Mar-02 4:55
moliate23-Mar-02 4:55 
GeneralRe: right to left layout Pin
23-Mar-02 2:11
suss23-Mar-02 2:11 
GeneralRe: right to left layout Pin
24-Mar-02 9:35
suss24-Mar-02 9:35 
Generalsetfocus Pin
23-Mar-02 0:25
suss23-Mar-02 0:25 
GeneralRe: setfocus Pin
Nish Nishant23-Mar-02 2:48
sitebuilderNish Nishant23-Mar-02 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.