473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 8828
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.