472,958 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 8798
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);

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;


}

Jul 23 '05 #5
>
void keyin (void *dummy)
{
//_getch();
if(_getch()=='q'||_getch()=='Q');
repeat=0;


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

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olivier Parisy | last post by:
Hi, I like to use thread to simplify the handling of independant, blocking tasks. But controling them from a main thread is not always easy to do in a clean way. So I've written some generic...
9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
5
by: iam1708 via DotNetMonster.com | last post by:
I was looking at the docs for Thread and can't understand the different between "unnamed data slot "and "data slot".and docs in Thread.GetData() say "Threads use a local store memory mechanism to...
8
by: JS | last post by:
I am monitoring/controlling some realtime activities in a manufacturing process. When a part comes into my station, I have a bunch of processing to do. There are 30-40 data acquisition and data...
6
by: Daniel | last post by:
i have an array that i want all threads to be able to READ from concurrently, however, at times i want to UPDATE the array. at which point i want all threads that use it to block when they try to...
37
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
11
by: Jon Slaughter | last post by:
Is there any way to start a terminated thread without using a pool or creating a new thread object? void counter() { clicks = 0; clock.Start(); while (counterActive) { clicks++;
16
by: Paul Schwann | last post by:
Hi group, I am relatively new to C# (although I have a lot of programming excperience in other languages like Java and C). Currently I am searching for a solution to this problem: Suppose you...
1
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread.Since my whole project is launched by windows service and that service will start automatically at the start of the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.