title: A thread-safe vector and string container?
body: <p>I posted a previous question <a href="https://stackoverflow.com/questions/2412667/seg-fault-when-using-stdstring-on-an-embedded-linux-platform">"Seg Fault when using std::string on an embedded Linux platform"</a> where I got some very useful advise. I have been away on other projects since then and have recently returned to looking at this issue.</p>
  
  <p>To reiterate, I am restricted to using the arm-linux cross compiler (version 2.95.2) as this is what is supplied and supported by the embedded platform vendor. I understand that the issue is likely because the stdlib is very old, and not particularly thread safe.</p>
  
  <p>The problem is that whenever I use the STL containers in multiple threads, I end up with a segmentation fault. The code below will consistently seg fault unless I use pthread_mutex_lock and scope operators around the container declarations (as in other post).</p>
  
  <p>It is not feasible to use this approach in my application as I pass the containers around to different methods and classes. I would ideally like to solve this problem, or find a suitable alternative. I have tried STLPort and SGI's Standard Template Library with the same results. I can only assume that because they are being linked by the very old gcc, they cannot solve the problem.</p>
  
  <p>Does anyone have any possible recommendations or solutions? Or perhaps you can suggest an implementation of vector (and string) that I can drop into my code?</p>
  
  <p>Thanks in advance for any guidance.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
    #include &lt;vector&gt;
    #include &lt;list&gt;
    #include &lt;string&gt;
  
    using namespace std;
      /////////////////////////////////////////////////////////////////////////////
  
      class TestSeg
      {
       static pthread_mutex_t     _logLock;
       public:
        TestSeg()
        {
        }
  
        ~TestSeg()
        {
        }
  
        static void* TestThread( void *arg )
        {
         int i = 0;
         while ( i++ &lt; 10000 )
         {
          printf( "%d\n", i );
          WriteBad( "Function" );
         }
         pthread_exit( NULL );
        }
  
        static void WriteBad( const char* sFunction )
        {
         //pthread_mutex_lock( &amp;_logLock );
         //{
  
         printf( "%s\n", sFunction );
         string sKiller;     //       &lt;----------------------------------Bad
         //list&lt;char&gt; killer;    //       &lt;----------------------------------Bad
         //vector&lt;char&gt; killer;    //       &lt;----------------------------------Bad
  
         //}
         //pthread_mutex_unlock( &amp;_logLock );
  
         return;
        }
  
        void RunTest()
        {
         int threads = 100;
         pthread_t     _rx_thread[threads];
         for ( int i = 0 ; i &lt; threads ; i++ )
         {
          pthread_create( &amp;_rx_thread[i], NULL, TestThread, NULL );
         }
  
         for ( int i = 0 ; i &lt; 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 );
      }
  </code></pre>