English 中文(简体)
vector形病媒和铺设集装箱?
原标题:A thread-safe vector and string container?

我在前一个问题上登出。 “...... Seg Fault, when using spind:string on an 嵌入式的六氯环己烷平台”,我在那里提出一些非常有用的建议。 我从那时起就停止了其他项目,最近又回到了研究这一问题。

我重申,我仅限于使用装甲交叉编印机(第2.95.2章),因为这是由嵌入平台的供应商提供和支持的。 我的理解是,这个问题之所以可能,是因为分界线非常老,而不是特别安全。

问题在于,每当我使用STL集装箱时,我最后会遇到分化问题。 除非我在集装箱申报单周围(如其他哨所)使用透镜_mutex_lock和广域操作者,否则以下代码将始终造成故障。

在我的申请中采用这种做法是不可行的,因为我把集装箱环绕到不同的方法和类别。 最好是解决这一问题,或找到合适的替代办法。 我尝试了STLPort和SGI标准模板图书馆,结果相同。 我只能假设,由于他们与旧的敌人有联系,他们不能解决问题。

是否有任何可能的建议或解决办法? 或者也许你可以建议把我可以引入我的守则的病媒(和扼杀)付诸实施?

事先得到任何指导。

#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
", i );
        WriteBad( "Function" );
       }
       pthread_exit( NULL );
      }

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

       printf( "%s
", 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 );
    }
问题回答

这一问题不在于集装箱,而是与你的编码相提。

完全没有必要使集装箱本身变得暗中,因为你们首先需要的是交易式杂质。

为证明起见,请假定您已准备好执行<条码>查询/代码>,例如。

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

这导致不明确的行为。

问题在于,在集装箱翻新线上进行每次操作都远远没有意义,因为你仍然需要能够把集装箱本身锁在一行的若干行动中。 因此,你将锁定你们的各种行动,然后又一次锁定每一项行动?

正如我所说的那样:完全没有必要。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签