[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Date and time classes not thread safe
From: |
David Sugar |
Subject: |
Re: Date and time classes not thread safe |
Date: |
Thu, 13 May 2004 11:36:06 -0400 |
User-agent: |
KMail/1.6.2 |
I will start testing this here...
On Thursday 13 May 2004 08:57 am, Idar Tollefsen wrote:
> Hello,
>
> time(), gettimeofday() and possibly localtime() are not thread safe
> functions. time() is, on some platforms at least, implemented by using
> gettimeofday().
>
> The problem, according to POSIX documentation, is that time(), and
> therefore gettimeofday(), keeps some static data that is overwritten by
> each call to the function. Needless to say, this caused some havoc when
> several threads in the same address space tried to use both the mentioned
> time functions and the ost::Time, ost::Date and ost::Datetime classes.
>
> I know some platforms have reentrant versions of the time functions with a
> _r suffix to the function name, but they aren't available on all platforms.
>
> My suggested solution is to introduce a ost::SysTime class that has a
> static semaphore and static functions that replaces the standard,
> non-thread safe, time functions, much like ost::Thread::sleep() does for
> the standard sleep() function. The functions in ost::SysTime then locks
> down access to the standard functions with the help of the semaphore.
>
> I have attached a patch that introduces this class and replaces all uses of
> time(), gettimeofday() and localtime() internally in CommonCpp2 with the
> equivalent function from ost::SysTime. The patch applies cleanly to a CVS
> checkout of commoncpp2 as of today, May 13th 14:15 GMT+1.
>
> I've used this patch for about a week now, and also replaced all calls to
> the standard time functions in our project with those introduced with
> ost::SysTime. This has solved all threading problems related to the use of
> time functions.
>
> Now, I'm not saying the attached solution is ideal. I would imagine it
> requires some more thought as to the function names and overall strategy.
> It does however highlight the problem and point out a possible solution.
> The patch also contains my fixes for ost::Time::getTime() and
> ThreadImpl::ThreadDestructor() that I sent in earlier as they hadn't been
> fixed in the CVS version (but really do cause problems if not fixed).
>
> The patch hasn't been tested on Win32 _at all_. I don't have access to a
> Windows machine with a development environment set up. It has seen most
> testing on OS X, some on FreeBSD and has been compiled on Linux.
>
> I can provide patches for the individual files instead if desired, just let
> me know.
>
>
> Sincerely,
> Idar Tollefsen