Will the current date / time be thread safe in C ++ 20?

Short question

Prior to and including C ++ 17, C ++ does not provide a thread-safe way to get the current time or date. Will this be fixed in C ++ 20?

Long question

The only portable way to get the current time and date is to use the functions std :: gmtime or std :: localtime. Residues from the first days of C, these functions will convert the specified time from the moment of implementation into calendar time (for example, 1515153600 on Fri, 05 January 2018 12:00:00 GMT). The only drawback, however, is that these functions return a pointer to an internal static variable and are not thread safe. Even worse, this static variable can be shared by all related functions, such as std :: gmtime, std :: localtime and std :: ctime, and can be overwritten each time any of these functions is called. Therefore, if you use threads and want to regularly check the time spent on risk, and the behavior is undefined.

Obviously, the current standard is violated in this regard. Is there any effort by the C ++ standard committee to correct this situation and how likely is it to be included in C ++ 20?

+7
c ++ date datetime time c ++ 20
source share
1 answer

Howard Hinnant's data library is what is suitable for C ++ 20. It was proposed through p0355r4 and approved for C ++ 20 in November 2017. Is it thread safe? Unfortunately, neither the documentation nor the proposal seems to be clear about this. However, some functions, such as get_tzdb_list , are explicitly called Thread Safety. Your best option is to ask Hinnant for a gitter chat. However, the discussion in Why is there no alternative to stf :: localtime and std :: gmtime? in C ++ 11 threadsafe, it seems that it is thread safe (even though it never explicitly spoke). As Nicol Bolas points out , you can just wrap it behind a mutex.

If this is not the case, you can get data races again and therefore undefined behavior. If you code large projects in which coding work is divided into teams, you should constantly remind everyone that they should not (!) Use the functions provided by the C ++ standard, but instead use their own shell (or risk undefined behavior) .

At a shallow level, this is what the codes say. Facebook has this problem for junior developers, where they continue to make the same mistakes over and over again. If your team has โ€œcurious repetitive errorsโ€, you need to access it somehow (for example, add checks for linter: Clang comes to mind).

At a much more direct level, Google is the epitome of this. The problem they encountered was using the old implementation of COW string and switching to string based SSO. However, since they relied on third-party libraries that used a COW-based string , they needed to maintain them in their code base. Telling developers about using Google wrappers was a futile effort. The solution that the developer came up with was hacked using inline namespaces . A bit extreme, but if you are dealing with an equally large code base, this can do the trick.

+8
source share

All Articles