Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Thread Start Time and End time

Reply
Thread Tools

Thread Start Time and End time

 
 
vee_kay
Guest
Posts: n/a
 
      03-09-2005
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...

 
Reply With Quote
 
 
 
 
Phil Staite
Guest
Posts: n/a
 
      03-09-2005
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.

 
Reply With Quote
 
 
 
 
vee_kay
Guest
Posts: n/a
 
      03-09-2005
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?

 
Reply With Quote
 
DHOLLINGSWORTH2
Guest
Posts: n/a
 
      03-09-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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?
>



 
Reply With Quote
 
vee_kay
Guest
Posts: n/a
 
      03-10-2005
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;






}

 
Reply With Quote
 
DHOLLINGSWORTH2
Guest
Posts: n/a
 
      03-11-2005
>
> void keyin (void *dummy)
> {
> //_getch();
>
>
> if(_getch()=='q'||_getch()=='Q');
> repeat=0;
>


what exactly is keyin supposed to do?


 
Reply With Quote
 
vee_kay
Guest
Posts: n/a
 
      03-11-2005
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;
}
}
}

 
Reply With Quote
 
DHOLLINGSWORTH2
Guest
Posts: n/a
 
      03-12-2005

"vee_kay" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Start a thread with the same credentials as the parent thread =?Utf-8?B?cGxleDRy?= ASP .Net 0 11-13-2007 08:11 PM
Threads: does Thread.start() atomically set Thread.__started ? Enigma Curry Python 1 03-15-2006 01:54 PM
thread end and make main program end also? martinnitram@excite.com Python 2 03-11-2005 02:43 AM
Specify start and length, beside start and end, in slices Noam Raphael Python 17 05-26-2004 09:30 PM
Passing result from one thread to another thread at the end of execution sayoyo Java 3 02-16-2004 03:39 PM



Advertisments