470,561 Members | 1,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,561 developers. It's quick & easy.

Thread Start Time and End time

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...

Jul 23 '05 #1
6 8634
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.

Jul 23 '05 #2
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?

Jul 23 '05 #3
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.
"vee_kay" <ve*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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?

Jul 23 '05 #4
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);

time (&start);
myhandle = (HANDLE) _beginthread( mythread,0,&val);

time (&end);

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

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


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




void keyin (void *dummy)


Jul 23 '05 #5
void keyin (void *dummy)

what exactly is keyin supposed to do?
Jul 23 '05 #6

"vee_kay" <ve*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
if(kbhit=='q'||kbhit=='Q') //if Q|q set, repeat=false and stop
thread and exit main
{ repeat=0;
////else if (kbhit !='q'||kbhit!='Q')//return true and keeps thread

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.

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Olivier Parisy | last post: by
9 posts views Thread by phil | last post: by
5 posts views Thread by iam1708 via DotNetMonster.com | last post: by
8 posts views Thread by JS | last post: by
6 posts views Thread by Daniel | last post: by
37 posts views Thread by ales | last post: by
51 posts views Thread by Hans | last post: by
11 posts views Thread by Jon Slaughter | last post: by
16 posts views Thread by Paul Schwann | last post: by
1 post views Thread by raghudr | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.