需要为Solaris 10上的GNU g++2.95.3设置线程安全的std::string
Need to put thread safe std::string for GNU g++ 2.95.3 on Solaris 10
std::string在Soalris 10(使用GNU g++2.95.3编译)上显示线程不安全行为。这是我的示例程序:
#include <iostream>
#include <string>
#include <pthread.h>
#include <stdio.h>
using namespace std;
void *Loop(void *) {
while(1) {
string *ps = new string("Hi");
if (ps == NULL) {
fprintf(stdout, "string creation failedn");
}}}
int main (int argc, char **argv) {
pthread_t thread1, thread2;
fprintf(stdout, "creating threadsn");
if(pthread_create(&thread1, NULL, Loop, NULL) == 0)
fprintf(stdout, "thread 1 createdn");
if(pthread_create(&thread2, NULL, Loop, NULL) == 0)
fprintf(stdout, "thread 2 createdn");
while(1);
return 0;
}
我将源代码(teststl.c)编译为:
g++ -c teststl.c
g++ -o teststl teststl.o -lthread
使用的平台和编译器有:
Platform: Solaris 10
Compiler GNU g++ 2.95.3
当我运行它时:它显示,
creating threads
thread 1 created
thread 2 created
./runteststl: line 5: 1412 Bus Error (core dumped) ./teststl
以下是"pstack core
"的转储
core 'core' of 14353: ./teststl
----------------- lwp# 1 / thread# 1 --------------------
00011a7c main (1, ffbff9cc, ffbff9d4, 232f8, ff2f00c0, 0) + b4
00011798 _start (0, 0, 0, 0, 0, 0) + 5c
----------------- lwp# 2 / thread# 2 --------------------
000121ec allocate__t24__default_alloc_template2b0i0Ui (20, 20, 23104, 69, 0, 0)
+ a4
00012220 __nw__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc
_template2b0i0_3RepUiUi (10, 10, ff000000, 0, 0, 1) + 14
00012260 create__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_all
oc_template2b0i0_3RepUi (2, 2, ff000000, 2, 1f, fffc00) + 24
000127a4 replace__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_allo
c_template2b0i0UiUiPCcUi (8df70, 0, ffffffff, 12a48, 2, 80808080) + 114
00012a24 assign__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc
_template2b0i0PCcUi (8df70, 12a48, 2, 0, ff1c0200, ff1b9210) + 24
000129e4 assign__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc
_template2b0i0PCc (8df70, 12a48, d9fd8, 129e4, ff1b03a8, ff1ba518) + 24
00012948 __t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_templ
ate2b0i0PCc (8df70, 12a48, 12800, 0, ff1c0200, 1) + 28
00011908 Loop__FPv (0, ff07c000, 0, 0, 118d0, 0) + 38
ff148a20 _lwp_start (0, 0, 0, 0, 0, 0)
----------------- lwp# 3 / thread# 3 --------------------
000121ec allocate__t24__default_alloc_template2b0i0Ui (20, 20, 23104, 69, 0, 0)
+ a4
00012220 __nw__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc
_template2b0i0_3RepUiUi (10, 10, ff000000, 0, 0, 1) + 14
00012260 create__Q2t12basic_string3ZcZt18string_char_traits1ZcZt24__default_all
oc_template2b0i0_3RepUi (2, 2, ff000000, 2, 1, fffc00) + 24
000127a4 replace__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_allo
c_template2b0i0UiUiPCcUi (8df60, 0, ffffffff, 12a48, 2, 80808080) + 114
00012a24 assign__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc
_template2b0i0PCcUi (8df60, 12a48, 2, 0, ff1c0a00, ff1b9210) + 24
000129e4 assign__t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc
_template2b0i0PCc (8df60, 12a48, d9fd8, 129e4, ff1b03a8, ff1ba518) + 24
00012948 __t12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_templ
ate2b0i0PCc (8df60, 12a48, 12800, 0, ff1c0a00, 1) + 28
00011908 Loop__FPv (0, fef7c000, 0, 0, 118d0, 0) + 38
ff148a20 _lwp_start (0, 0, 0, 0, 0, 0)
这显示了一个争用问题。与编译或链接标志有关吗?尝试了来自的建议https://docs.oracle.com/cd/E19455-01/806-5257/compile-94179/index.html但这也没用。有什么建议吗?
崩溃发生在std::allocator
内部,所以我认为这在GCC 2.95.3中不是正确的线程安全的,但我不会挖掘出古老的代码进行检查。
停止使用这样的遗迹。
您的编译/链接开关似乎不正确。您需要将-ppthreads添加到编译和链接步骤中。如果没有这些,线程不安全代码将用于所有级别,包括内存分配。
相关文章:
- 将更高的优先级设置为 boost::asio 线程处理进程
- 是否可以在单独的线程中将 QObject 设置为 QML 上下文属性?
- 如何使用 pthreads 以正确的方式设置两个线程之一的优先级
- 设置 JVM 通过 JNI 初始化时创建的线程数
- 设置提升记录器的线程名称
- Android Ndk - 为C++线程设置名称
- 如何设置"this"线程的自定义名称?
- 如何在 OpenMP 中设置线程数
- 在 Visual Studio 中的调试中断时设置默认线程,C++
- OpenBLAS 只为一个例程设置线程数
- 在iOS设备上执行并发任务时如何设置正确的线程数?
- 如何在不需要 root 访问权限的情况下为应用程序中的线程设置相对线程优先级
- 在C 多线程应用程序中,设置了并发线程的最大数量
- 在 OpenCV 上设置线程相关性
- 如何在 Qt5 中设置线程的 CPU 关联?
- 一个线程设置成员,而另一个循环上方 - 是此螺纹 - 不安全
- 不使用线程时,从不同的线程设置Qt父级
- 为线程设置新优先级时不允许操作
- 跨多个线程设置表项
- 从创建线程设置线程的"nice level"?