🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #6886

get time problem

#6886
From: Alex Liu
Date: Thu, 31 Mar 2005 08:13
69 lines
2772 bytes
Hi,

I am writing a multi-threaded time-critical application in which every
thread will write some logs to a log file. I defined a global function to
write logs in this way:

void __fastcall TGlobal::WriteLog(AnsiString msg)
{
    CriticalSection->Enter();
    try
    {
        mylogfile->Write(FormatDateTime("hh:nn:ss.zzz", Now()), msg);
    }
    __finally
    {
        CriticalSection->Leave();
    }
}

And generally my threads work in this way:

while(!Terminated)
{
    MyGlobal->WriteLog("thread start job");
    dosomefcn();

    MyGlobal->WriteLog("thread finish job");
    Sleep(1);
}

where MyGlobal is passed as a reference, inited globally.

Here is the strange part (well, I feel it strange). The log shows time like
this:
14:37:01.984 thread_A start job
14:37:01.984 thread_A finish job
14:37:01.984 thread_B start job
14:37:01.984 thread_B finish job
14:37:01.984 thread_C start job
14:37:01.984 thread_C finish job
14:37:02.312 thread_A start job
14:37:02.312 thread_A finish job
14:37:02.312 thread_A start job
14:37:02.312 thread_A finish job
14:37:02.312 thread_B start job
14:37:02.312 thread_B finish job
14:37:02.312 thread_B start job
14:37:02.312 thread_B finish job
14:37:02.328 thread_C start job
14:37:02.328 thread_C finish job
14:37:02.343 thread_A start job
14:37:02.343 thread_A finish job
14:37:02.343 thread_A start job
14:37:02.343 thread_A finish job
14:37:02.343 thread_B start job
14:37:02.343 thread_B finish job
14:37:02.343 thread_B start job
14:37:02.343 thread_B finish job
14:37:02.359 thread_C start job

I feel strange that the time is the same for a period of time and over and
over again. Is this normal?
And i found that the time difference is always about 0.016s. Why? (this
confused me a lot as i cant accurately measure time used for a function)
Does it exist a more accurate way to log the time to milliseconds?

Thanks in advance.

Alex
Technical Details
Message-ID: <424bb15b@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e46af9f8f318731f,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Alex Liu" <alexliu@hket.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: get time problem
Date: Thu, 31 Mar 2005 16:13:53 +0800
Lines: 72
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 202.131.71.54
Message-ID: <424bb15b@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1112256859 202.131.71.54 (31 Mar 2005 00:14:19 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news2.google.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail