Thread Start Time and End time

Discussion in 'C++' started by vee_kay, Mar 9, 2005.

  1. vee_kay

    vee_kay Guest

    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...
     
    vee_kay, Mar 9, 2005
    #1
    1. Advertising

  2. vee_kay

    Phil Staite Guest

    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.
     
    Phil Staite, Mar 9, 2005
    #2
    1. Advertising

  3. vee_kay

    vee_kay Guest

    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?
     
    vee_kay, Mar 9, 2005
    #3
  4. 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" <> wrote in message
    news:...
    > 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?
    >
     
    DHOLLINGSWORTH2, Mar 9, 2005
    #4
  5. vee_kay

    vee_kay Guest

    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;






    }
     
    vee_kay, Mar 10, 2005
    #5
  6. >
    > void keyin (void *dummy)
    > {
    > //_getch();
    >
    >
    > if(_getch()=='q'||_getch()=='Q');
    > repeat=0;
    >


    what exactly is keyin supposed to do?
     
    DHOLLINGSWORTH2, Mar 11, 2005
    #6
  7. vee_kay

    vee_kay Guest

    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;
    }
    }
    }
     
    vee_kay, Mar 11, 2005
    #7
  8. "vee_kay" <> wrote in message
    news:...
    > 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.
     
    DHOLLINGSWORTH2, Mar 12, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sayoyo
    Replies:
    3
    Views:
    1,275
    sayoyo
    Feb 16, 2004
  2. Noam Raphael
    Replies:
    17
    Views:
    543
    Noam Raphael
    May 26, 2004
  3. Replies:
    2
    Views:
    367
  4. Enigma Curry
    Replies:
    1
    Views:
    514
    Peter Hansen
    Mar 15, 2006
  5. =?Utf-8?B?cGxleDRy?=
    Replies:
    0
    Views:
    369
    =?Utf-8?B?cGxleDRy?=
    Nov 13, 2007
Loading...

Share This Page