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... 6 8865
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.
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?
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*********@gm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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?
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(ke yin, 0, NULL);
while(repeat)
{
time (&start);
Sleep(1000L);
myhandle = (HANDLE) _beginthread( mythread,0,&val );
time (&end);
dif=difftime(en d, start);
_strtime( timeStr );
ofc = fopen(ofilename ,"a+");
if(dif<=1)
{
WaitForSingleOb ject(myhandle,I NFINITE);
printf("Dif: %.3lf s\n",dif);
}
else
{
printf("Less than a second Time:%s\n", timeStr);
fprintf(ofc,"Cu rrent 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*)param eter);
FILE *ofp; //FILE IO
ofp = fopen(ofilename ,"a+"); //FILE IO
_strdate( dateStr);
_strtime( timeStr );
printf("Current Thread ---> Time:%s Date:%s \n",dateStr,tim eStr);
fprintf(ofp,"Cu rrent Thread ---> Time:%s Date:%s
\n",dateStr,tim eStr); //FILE IO
fclose(ofp);
_endthread();
}
void keyin (void *dummy)
{
//_getch();
if(_getch()=='q '||_getch()=='Q ');
repeat=0;
}
> void keyin (void *dummy) { //_getch();
if(_getch()=='q '||_getch()=='Q '); repeat=0;
what exactly is keyin supposed to do?
"vee_kay" <ve*********@gm ail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 code whose purpose is to start and stop threads in
a synchronous (blocking) way from the caller's point of view.
Hence, after start() is called you are garanteed that the
thread is running, and after stop() you know it has completed
|
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, you've
>provided next to no useful (IMHO) information to
>let anyone help you more than this
This is about 5% of the code.
Uses no locks.
|
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 store thread-specific
data." what is "thread-specific data"?
Best regards
wmt
email:iam1708@hotmail.com
|
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 processing steps that need to
be performed. Many of the steps rely on the results of other steps for
their processing.
I have each step coded as a separate instance. The way I'd like to
execute all the steps is to spawn a thread for each of...
|
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 read from the array.
currently i do this by wrapping ALL READ AND WRITE access to the array in a
lock("foobar"){} this prevernts threads from reading from the array while it
is being written to however it ALSO keeps more then one thread from...
| |
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 is code used for measuring:
|
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() # This thread is expected to finish within a second
|
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++;
|
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 have 3 methods A, B and C. All of them shall be run
threaded (I/O tasks) and one after another. I can create a thread and
schedule one method just like:
|
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 PC and sometimes when i start or
restart the PC i have observed that even though the long process is
completed splash
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |