Safe vector and string container?

I posted the previous question, “Seg Fault when using std :: string on the embedded Linux platform,” where I got some useful tips. Since then I have been absent from other projects and recently returned to the consideration of this problem.

I repeat, I am limited to using the arm-linux cross compiler (version 2.95.2), since this is what is supplied and supported by the provider of the embedded platform. I understand that the problem is most likely due to the fact that stdlib is very old and not particularly thread safe.

The problem is that whenever I use STL containers in multiple threads, I get a segmentation error. An error will be sequentially detected in the code below if I do not use pthread_mutex_lock and scope operators around container declarations (as in other posts).

It is not possible to use this approach in my application, as I pass containers to various methods and classes. I would ideally like to solve this problem or find a suitable alternative. I tried the standard STLPort and SGI template library with the same results. I can only assume that since they are connected by a very old gcc, they cannot solve the problem.

Does anyone have any possible recommendations or solutions? Or maybe you can suggest an implementation of a vector (and a string) that I can insert into my code?

Thanks in advance for any recommendations.

#include <stdio.h>
  #include <vector>
  #include <list>
  #include <string>

  using namespace std;
    /////////////////////////////////////////////////////////////////////////////

    class TestSeg
    {
     static pthread_mutex_t     _logLock;
     public:
      TestSeg()
      {
      }

      ~TestSeg()
      {
      }

      static void* TestThread( void *arg )
      {
       int i = 0;
       while ( i++ < 10000 )
       {
        printf( "%d\n", i );
        WriteBad( "Function" );
       }
       pthread_exit( NULL );
      }

      static void WriteBad( const char* sFunction )
      {
       //pthread_mutex_lock( &_logLock );
       //{

       printf( "%s\n", sFunction );
       string sKiller;     //       <----------------------------------Bad
       //list<char> killer;    //       <----------------------------------Bad
       //vector<char> killer;    //       <----------------------------------Bad

       //}
       //pthread_mutex_unlock( &_logLock );

       return;
      }

      void RunTest()
      {
       int threads = 100;
       pthread_t     _rx_thread[threads];
       for ( int i = 0 ; i < threads ; i++ )
       {
        pthread_create( &_rx_thread[i], NULL, TestThread, NULL );
       }

       for ( int i = 0 ; i < threads ; i++ )
       {
        pthread_join( _rx_thread[i], NULL );
       }
      }

    };

    pthread_mutex_t       TestSeg::_logLock = PTHREAD_MUTEX_INITIALIZER;

    int main( int argc, char *argv[] )
    {
     TestSeg seg;
     seg.RunTest();
     pthread_exit( NULL );
    }
+2
source share
2 answers

The problem is not in containers, but in code.

It is not necessary that the containers themselves be thread safe, because first of all you need transactional semantics.

Suppose, for demonstration, that you, for example, use a stream implementation vector.

  • Theme 1: if (!vec.empty())
  • Theme 2: vec.clear();
  • Theme 1: foo = vec.front();

This leads to undefined behavior.

, , - . , ?

: .

+6

. ++, , . - . . .

STL ( Decorator) / .

+2

Source: https://habr.com/ru/post/1761742/


All Articles