lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello, everyone!

I have encountered an issue with os.date(). As it's commonly known, Lua is pretty much thread-safe when properly handled. However, you can't guarantee thread safety of some library functions which hold state like os.date and math.random().

Personally, I totally am ok with math.random() and I can accept its thread-unsafety, i.e. issuing the same random number in multiple threads. However, it's worse with os.date() which will issue completely _WRONG_ results because the internal time structure is filled by racing calls.

The best way would be to use localtime_r() instead of localtime(). The manual states it is sufficient to check for _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE.

The other way would be to write my own os.date(), although it looks the patch could be useful to many people because the issue is really rare but devastating.

Your opinions are welcome. Thanks!

// Seny