how to schedule the compiler?

C

cdrsir

I tried the following code, which prints some message on the screen for
every minute, but this takes the whole CPU, who can tell me how to
change the code, so that it takes not so much CPU when running the
program. Thanks.


#include "stdio.h"
#include "time.h"

void main()
{
struct tm *local;
int actual_minu;
int last_minu;
time_t t;

//! initialize the actual_minu and last_minu
t = time(NULL);
local = localtime(&t);
actual_minu = local->tm_min;
last_minu = actual_minu;

while ( true )
{
t = time(NULL);
local = localtime(&t);
actual_minu = local->tm_min;
if ( actual_minu-last_minu >= 1 ) {
printf("One minute is gone!!!\n");
last_minu = actual_minu;
}
}

}
 
M

mlimber

cdrsir said:
I tried the following code, which prints some message on the screen for
every minute, but this takes the whole CPU, who can tell me how to
change the code, so that it takes not so much CPU when running the
program. Thanks.


#include "stdio.h"
#include "time.h"

void main()
{
struct tm *local;
int actual_minu;
int last_minu;
time_t t;

//! initialize the actual_minu and last_minu
t = time(NULL);
local = localtime(&t);
actual_minu = local->tm_min;
last_minu = actual_minu;

while ( true )
{
t = time(NULL);
local = localtime(&t);
actual_minu = local->tm_min;
if ( actual_minu-last_minu >= 1 ) {
printf("One minute is gone!!!\n");
last_minu = actual_minu;
}
}

}

There is no way to do this in a platform-independent fashion. Ask in a
newsgroup for your compiler or OS. See this FAQ for some possible
groups to post to:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

Cheers! --M
 
C

cdrsir

may be you are right, but hopfully there is someone who has some ideas.

I use VC++ 6.0, Win-XP
 
M

mfurnari

Have you tried using a "Sleep" function instead of busy waiting.
Something like this:

for (int i = 0; ;i++)
{
Sleep(60000); //One minute = 1000 ms/s * 60 s/m
cout << i << " minutes have passed" << endl;
}
 
S

steve.holle

Is there a "sleep" command you could use to suspend your ap for a
specific period?
 
C

cdrsir

I think Sleep is exactly the function that I need. thanks a lot.


(e-mail address removed) 写é“:
 
R

red floyd

cdrsir said:
I tried the following code, which prints some message on the screen for
every minute, but this takes the whole CPU, who can tell me how to
change the code, so that it takes not so much CPU when running the
program. Thanks.


#include "stdio.h"
#include "time.h"

void main()
{
struct tm *local;
int actual_minu;
int last_minu;
time_t t;

//! initialize the actual_minu and last_minu
t = time(NULL);
local = localtime(&t);
actual_minu = local->tm_min;
last_minu = actual_minu;

while ( true )
{
t = time(NULL);
local = localtime(&t);
actual_minu = local->tm_min;
if ( actual_minu-last_minu >= 1 ) {
printf("One minute is gone!!!\n");
last_minu = actual_minu;
}
}

}


1. This is C code, not C++.
2. main() returns int, not void.
 
D

Diego Martins

each OS has a great variety of "sleep like" functions

with threads, this is an issue worth of investigation (and hard to make
it portable) :(
 
C

cdrsir

I would like to use

#ifdefine windows
call your windows related function
.....



or ifdefine linux
and call your linux related function

to handel the different system

Diego Martins 写é“:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top