Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost ptime under MinGW not thread safe

Tags:

c++

mingw

boost

I have a problem with boost library. I'm using MinGW with gcc 4.5.2 to compile the following code:

unsigned long GetEpochSeconds()
{
    using namespace boost::posix_time;
    using namespace boost::gregorian;

    ptime now(second_clock::universal_time());
    ptime epoch(date(1970,1,1));
    time_duration diff = now-epoch;
    return diff.total_seconds();
}

The problem is that this code is not thread-safe. When I run it from within multiple threads, my application crashes. For now I've converted to c-standard functions like time, mktime etc. and everything works fine, but in the future I will need a few boost time functions.

I was compiling also with -D_REENTRANT, but this didn't help.

Thanks for any suggestions.

like image 735
kappa Avatar asked Oct 28 '11 07:10

kappa


1 Answers

Check whether your code is calling gmtime() or gmtime_r() (use a debugger for this). See http://www.boost.org/doc/libs/1_48_0/boost/date_time/c_time.hpp and note that BOOST_DATE_TIME_HAS_REENTRANT_STD_FUNCTIONS must be defined in order for getting the time to be thread-safe.

like image 54
John Zwinck Avatar answered Oct 11 '22 08:10

John Zwinck