为什么达尔文的斯特托德线不安全?
Why isn't Darwin's strtod thread safe?
以下测试在我的英特尔Mac Mini上总是会产生故障或总线错误。
编译器:
uname -a:
Darwin vogon13 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386
g++ -v:
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5493~1/src/configure
--disable-checking -enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.0/
--with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib
--build=i686-apple-darwin9 --with-arch=apple --with-tune=generic
--host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5493)
编译命令:
"g++" -ftemplate-depth-128 -O3 -finline-functions -Wno-inline -Wall
-pedantic -g -no-cpp-precomp -gdwarf-2 -Wno-long-double -Wno-long-long -fPIC
-DBOOST_DATE_TIME_NO_LIB=1 -DBOOST_THREAD_NO_LIB=1 -DBOOST_THREAD_USE_LIB=1
-DDATE_TIME_INLINE -DNDEBUG -I"." -c -o "strtod_test.o" "strtod_test.cpp"
"g++" -o "strtod_test" "strtod_test.o"
"libboost_thread-xgcc40-mt-s.a" "libboost_date_time-xgcc40-mt-s.a"
-g -nodefaultlibs -shared-libgcc -lstdc++-static -lgcc_eh -lgcc -lSystem
-Wl,-dead_strip -no_dead_strip_inits_and_terms -fPIC
源代码:
#include "boost/thread/thread.hpp"
#include "boost/thread/barrier.hpp"
#include <iostream>
using namespace std;
void testThreadSafetyWorker(boost::barrier* testBarrier)
{
testBarrier->wait(); // wait until all threads have started
try
{
const char* str = "1234.5678";
const char* end = str;
double result = strtod(str, const_cast<char**>(&end));
if (fabs(result-1234.5678) > 1e-6)
throw runtime_error("result is wrong!");
}
catch (exception& e) {cerr << "Exception in worker thread: " << e.what() << endl;}
catch (...) {cerr << "Unhandled exception in worker thread." << endl;}
}
void testThreadSafety(const int& testThreadCount)
{
boost::barrier testBarrier(testThreadCount);
boost::thread_group testThreadGroup;
for (int i=0; i < testThreadCount; ++i)
testThreadGroup.add_thread(new boost::thread(&testThreadSafetyWorker, &testBarrier));
testThreadGroup.join_all();
}
int main(int argc, char* argv[])
{
try
{
testThreadSafety(2);
testThreadSafety(4);
testThreadSafety(8);
testThreadSafety(16);
return 0;
}
catch (exception& e) {cerr << e.what() << endl;}
catch (...) {cerr << "Unhandled exception in main thread." << endl;}
return 1;
}
堆栈跟踪:
(gdb) bt
#0 0x950c8f30 in strlen ()
#1 0x9518c16d in strtod_l$UNIX2003 ()
#2 0x9518d2e0 in strtod$UNIX2003 ()
#3 0x00001950 in testThreadSafetyWorker (testBarrier=0xbffff850) at strtod_test.cpp:21
#4 0x0000545d in thread_proxy (param=0xffffffff) at boost_1_43_0/libs/thread/src/pthread/thread.cpp:12? ?1
#5 0x950f1155 in _pthread_start () #6 0x950f1012 in thread_start ()
strtod不是线程安全的。它从未在任何规格中说明它是线程安全的。您应该使用strtod_l,它需要一个区域设置。在所有的likelehood中,都有一个共享数据结构,它在线程代码中的使用正在破坏它。
locale_t c_locale = newlocale (LC_ALL_MASK, "C", 0);
r = strtod_l(nptr, endptr, c_locale);
freelocale (c_locale);
相关文章:
- C++/CLI 和 C#/VB 与不安全和外部有什么区别?
- 问:Apache Arrow 数组生成器不安全追加
- 不安全的 MPI 非阻塞通信示例?
- 有没有一种简单的方法来检查C++中的不安全表达式
- 为什么静态向下转换unique_ptr不安全?
- 哪些整数操作不安全
- 为什么这个递归 lambda 函数不安全?
- 解决方法:QPixmap:在GUI线程之外使用pixmap是不安全的
- 正在匹配不安全的正则线程
- 如何修复编译错误"此函数或变量可能不安全"(strcpy)
- 编译器在 const ref 类型参数上使用临时对象时是否应该警告不安全的行为?
- 实现没有不安全服务器凭据的自定义 AuthMetadataProcessor
- 什么时候关闭__strict_ansi__标志是不安全的
- 原子对象在普通对象安全的任何上下文中都是不安全的
- OpenSSL:将不安全的BIO提升为安全
- 这是对支撑初始器列表的不安全使用情况
- 从 C# 到C++和返回的数组,没有不安全的代码
- 一个线程设置成员,而另一个循环上方 - 是此螺纹 - 不安全
- 访问"std::variant"的不安全、"noexcept"和无开销方式
- 为什么达尔文的斯特托德线不安全?