c++ Why is my date parsing not threadsafe?

boost::posix_time::ptime parseDate(const std::string& format, const std::string& localDate)
{
    std::istringstream is(localDate);
    is.imbue(std::locale(is.getloc(), new boost::local_time::local_time_input_facet(format.c_str())));
    boost::posix_time::ptime pt;
    is >> pt;

    if (pt == boost::posix_time::ptime())
    {
        throw std::runtime_error("Parse error");
    }

    return pt;
}

This function should take a date and a format string and return boost::posix_time::ptime.

E.g.: 2012:06:14 02:50:58 and %Y:%m:%d %H:%M:%S.

However if I call it in a multithreaded program, sometimes the exception is thrown, although format and localDate are correct and parseable (I use the same date for every call). I found something about std::stringstream/std::locale thread issues but nothing up-to-date (I am using gcc 4.6.3 64bit).

Here someone has the same problem:

Testing over the last few days using Valgrind/drd, I have found many parts of my code that cause problems. For example, when calling some boost date time conversion functions, I hit std::locale(), which is not threadsafe.

Updated code that gives no problems:

boost::posix_time::ptime parseDate(const std::string& format, const std::string& localDate)
{
    std::istringstream is(localDate);
    auto* facet = new boost::local_time::local_time_input_facet(format.c_str());

    {
        boost::unique_lock<boost::mutex> lock(globalLocaleMutex);
        is.imbue(std::locale(is.getloc(), facet));
    }

    boost::posix_time::ptime pt;
    is >> pt;

    if (pt == boost::posix_time::ptime())
    {
        throw std::runtime_error("Parse error");
    }

    return pt;
}

But still: Why?

Is my class threadsafe? If not why?

Is my class threadsafe? If not why? class Foo { boolean b = false; void doSomething() throws Exception { while (b) Thread.sleep(); } void setB(boolean b) { this.b = b; } }

Why isn’t my date parsing correctly?

I’m having issues parsing dates after 01/01/2000. The results are being returned incorrectly. 1999 is getting parsed as the year 1999, when it gets to 2000 it is parsing it as 0100, and then 2001 as 0

Why my date is not parsing

private final SimpleDateFormat gmailDateFormatter = new SimpleDateFormat(EEE, d MMM yyyy HH:mm:ss Z); But when i parse the date String Thu, 25 Sep 2014 16:26:20 +0300 i get the following exceptio

Why is SimpleDateFormat not parsing my date correctly?

I’m trying to avoid reinstalling Eclipse to solve this, so I hope someone can help with this date-parsing problem. Here’s what I do. DateFormat dateFormat; That’s a variable in my class. I set it to

c++: is this getInstance() method of my singleton threadsafe?

I am currently trying to write a threadsafe singleton (at least in terms of construction and destruction) using boost::mutex. I read that boost mutex can’t be initialized statically(I lost the link wh

Date/Time parsing in C++

While doing the data/time parsing in c++ (converting a string in any format to a date), i found the following useful methods 1) strptime() – here the %d, %m etc can have either 1 or 2 characters. The

Is there a threadsafe and generic IList in c#?

Is List<T> or HashSet<T> or anything else built in threadsafe for addition only? My question is similar to http://stackoverflow.com/questions/1278010/threadsafe-and-generic-arraylist-in-c

C Library for Parsing Date Time

Is one aware of a date parsing function for c. I am looking for something like: time = parse_time(9/10/2009); printf(%d/n, time->date); time2 = parse_time(Monday September 10th 2009) time2 =

Why are static fields generally considered threadsafe?

I’m a .NET and Java developer who is relatively new to threading. I’d like to know why static fields, static constructors, etc are generally considered threadsafe. For example, when constructing a

Threadsafe over Bitmap in C#

Can anyone tell me why this code is wrong and how I have to modify it in order to create a simple Bitmap of which is guranteed thread-safe access? public static class ThreadSafe { public static readon

Answers

I see there is a call to local_time. I am not sure if the underlying code calls localtime or localtime_r. If it calls localtime, then it is not thread safe. I believe that localtime uses a static variable when it returns the result.