簡體   English   中英

需要在Solaris 10上將線程安全std :: string用於GNU g ++ 2.95.3

[英]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 failed\n");
}}}
int main (int argc, char **argv) {
 pthread_t thread1, thread2;
 fprintf(stdout, "creating threads\n");
 if(pthread_create(&thread1, NULL, Loop, NULL) == 0)
  fprintf(stdout, "thread 1 created\n");
 if(pthread_create(&thread2, NULL, Loop, NULL) == 0)
  fprintf(stdout, "thread 2 created\n");
  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中不是線程安全的,但是我不會挖掘出要檢查的古老代碼。

停止使用這樣的遺物。

您的編譯/鏈接開關似乎不正確。 您需要將-pthreads添加到編譯和鏈接步驟。 沒有那些,線程不安全代碼將在所有級別上使用,包括內存分配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM