KStringHandler: stateless/reentrant/thread-safe?

Frank Reininghaus frank78ac at googlemail.com
Sun Oct 28 16:12:31 GMT 2012


2012/10/27 Thiago Macieira:
> On sexta-feira, 26 de outubro de 2012 22.44.23, Thiago Macieira wrote:
>> On sexta-feira, 26 de outubro de 2012 21.06.35, Frank Reininghaus wrote:
>> > Yes, I thought the same, but then I wasn't entirely sure if all these
>> > functions really are thread-safe. Some of them have const static
>> > variables,
>> > which might be initialised the first time the function is called AFAIK. I
>> > don't know if that's guaranteed to work if the functions can be called
>> > from
>> > multiple threads. But you can probably answer that question
>>
>> Caching some global information which could be recreated on every call is
>> stateless and thread-safe.
>
> Actually, this is wrong. Caching is a good thing, but it could cause a thread-
> safety issue if not implemented correctly.
>
> The presence of statics probably requires a good investigation to check if
> it's thread-safe. And removing the "stateless" requirement is still a good
> idea.

The functions with statics are:

1. KStringHandler::isUtf8(const char *)

http://api.kde.org/4.9-api/kdelibs-apidocs/kdecore/html/kstringhandler_8cpp_source.html#l00204

2. KStringHandler::from8Bit(const char *)

http://api.kde.org/4.9-api/kdelibs-apidocs/kdecore/html/kstringhandler_8cpp_source.html#l00289

The first one contains a static array of chars, which looks safe to
me. The second has a static reference to a QString which is generated
using KGlobal::staticQString(const char*). I'm not entirely sure if
that is safe as well.

Regards,
Frank




More information about the kde-core-devel mailing list