Thread Start Time and End time

V

vee_kay

Ihave a written aprogram in C which implements _beginthread(to create a
thread) and _endthread(to end a thread).The program need to write a
string of date n time to a file for each succesful thread created. I
had put a delay of a second so that the thread and io operation will
occur after a second. Now i need to implement another thing which i
need to make sure the run was actually a second. This is because if i
add another delay of 400milisecond and if i looked into my log file i
would miss few seconds. Instead of normal each second recording , i
would miss few seconds in a minute..I guess you guys do understand what
i am saying here..Pls help...
 
P

Phil Staite

You're going to have to take a hard look at your operating system first
and foremost. Your typical desktop OS is non real-time. It's process
and thread schedulers probably make no guarantee about latency,
determinism, etc. For example, it's not uncommon for Windows or Linux
to occasionally take long siestas from user code while some OS task runs.
 
V

vee_kay

Any other way to do this..as i dont really get what ur really trying to
say .....a simple program need to see what is OS doing eh?
 
D

DHOLLINGSWORTH2

Timing things on a computer is difficult at best, You are not going to be
very accurate in a short amount of time.

I'm NOT sure what you are asking but,
What most pro's do is run a chunck of code for say 1000 iterations, then
deviding the total time by 1000 to get an average time elapsed.

You will find that the time between beginthread and endthread, varies with
every run.
 
V

vee_kay

OK..i need to make sure that even after a delay of 400milisecond ...i
have to make sure that it wont skip a second..I include my code and try
c if u guys can correct it...

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <time.h>

void mythread(void *parameter);
void keyin (void *dummy);

BOOL repeat=TRUE;

int main()
{
int val = 0;
double dif;
char timeStr [9];
char ofilename[] = "ThreadNotDone.txt";

time_t start,end;

FILE *ofc;
HANDLE myhandle;

_beginthread(keyin, 0, NULL);

while(repeat)
{
time (&start);
Sleep(1000L);
myhandle = (HANDLE) _beginthread( mythread,0,&val);

time (&end);

dif=difftime(end, start);
_strtime( timeStr );
ofc = fopen(ofilename,"a+");
if(dif<=1)
{

WaitForSingleObject(myhandle,INFINITE);
printf("Dif: %.3lf s\n",dif);
}
else
{
printf("Less than a second Time:%s\n", timeStr);
fprintf(ofc,"Current Thread ---> Less Than A Second Time:%s \n
",timeStr);
}

}

return 0;
}

void mythread(void *parameter)
{

char dateStr [9];
char timeStr [9];


char ofilename[] = "Thread.txt"; //FILE IO

int h=*((int*)parameter);

FILE *ofp; //FILE IO

ofp = fopen(ofilename,"a+"); //FILE IO

_strdate( dateStr);
_strtime( timeStr );


printf("Current Thread ---> Time:%s Date:%s \n",dateStr,timeStr);

fprintf(ofp,"Current Thread ---> Time:%s Date:%s
\n",dateStr,timeStr); //FILE IO

fclose(ofp);

_endthread();

}

void keyin (void *dummy)
{
//_getch();


if(_getch()=='q'||_getch()=='Q');
repeat=0;






}
 
V

vee_kay

well actually that part of the code suppose to be like this....
void keyin (void *dummy)
{
char kbhit;
while (kbhit=_getch())//get a char from user, no char, thread keep on
repeat
{
if(kbhit=='q'||kbhit=='Q') //if Q|q set, repeat=false and stop
thread and exit main
{ repeat=0;
break;
}
else if (kbhit !='q'||kbhit!='Q')//return true and keeps thread
running
{
repeat=TRUE;
continue;
}
}
}
 
D

DHOLLINGSWORTH2

vee_kay said:
well actually that part of the code suppose to be like this....
void keyin (void *dummy)
{
char kbhit;
while (kbhit=_getch())//get a char from user, no char, thread keep on
repeat
{
if(kbhit=='q'||kbhit=='Q') //if Q|q set, repeat=false and stop
thread and exit main
{ repeat=0;
//break;
}
////else if (kbhit !='q'||kbhit!='Q')//return true and keeps thread
////running
////{
////repeat=TRUE;
////continue;
////}
}
}

The commented code above is no longer needed. Once you set Repeat, it's not
going to change unless you set it different.

Redundant code.

I believe if you start at the begining, and code it one more timeYou'll have
it figured out on your own.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top