是setlocale线程安全的功能? [英] Is setlocale thread-safe function?

查看:562
本文介绍了是setlocale线程安全的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改线程中的区域设置,以正确地解析strtod(),我正在使用setlocale()(C ++)。是线程安全吗?



更新:另一个问题。当我在main()函数中调用setlocale()时,它在其他例程中不会更深入。为什么???有很多代码,所以编写这个块是有问题的。

解决方案

您需要查阅所使用的任何实现的文档。 C ++目前没有指定任何关于线程的内容,因此它归结于实现(您还没有告诉我们)。



例如,我的Linux的帮助页面setlocale具有代码段:


此字符串可能会分配静态存储。


这不绝对表明它是线程不安全的,但我会非常警惕。 $ b

可能是最安全的事情(假设它不是线程安全的)将保护所有调用到code> setlocale 与互斥体,并具有特殊功能,可以按照以下方式格式化数字:

 声明互斥体
curr = setlocale具体值
格式号到字符串
setlocale到curr
释放mutex
返回字符串


I need to change locale in the thread to parse a double with strtod() correctly, I'm using setlocale() for this (C++). Is it thread safe?

Update: Another problem. When I invoke setlocale() in my main() function it doesn't affect in other routines deeper. Why??? There is a lot of code, so it's problematic to write the chunk.

解决方案

You need to consult the documentation for whatever implementation you're using. C++ doesn't currently specify anything about threads so it comes down to the implementation (which you haven't yet told us).

For example, my Linux manpage for setlocale has the snippet:

This string may be allocated in static storage.

which doesn't absolutely indicate that it's thread-unsafe but I'd be very wary. It's likely that calling it with NULL (i.e., querying) would be thread-safe but as soon as you have a thread modifying it, all bets are off.

Probably the safest thing to do (assuming it isn't thread-safe) would be to protect all calls to setlocale with a mutex and have a special function to format your numbers along the lines of:

claim mutex
curr = setlocale to specific value
format number to string
setlocale to curr
release mutex
return string

这篇关于是setlocale线程安全的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆