相关疑难解决方法(0)

C++ std :: set线程安全吗?

我有一个关于std :: set的线程安全性的问题.

据我所知,我可以遍历一个集合并添加/擦除成员,这不会使迭代器无效.

但请考虑以下情况:

  • 线程'A'遍历一组shared_ptr <Type>
  • 线程'B'偶尔会将项目添加到此集合中.

程序运行时我经历过段错误,我不确定为什么会这样.缺乏线程安全的原因是什么?

c++ stl std stdset

36
推荐指数
3
解决办法
3万
查看次数

用于C++的Threadsafe Vector类

有谁知道c ++的快速和脏线程安全矢量类?我正在多线程处理一些代码,我相信我遇到的问题与使用向量的方式有关.我打算重写代码,但在我疯狂重做代码之前,我想用线程安全向量来测试它.我还想知道如果有这样的东西,它会比写我自己的版本容易得多.

c++ multithreading thread-safety

14
推荐指数
2
解决办法
2万
查看次数

在嵌入式Linux平台上使用std :: string时出现Seg Fault

我已经在我的应用程序在嵌入式Arm Linux平台上运行的问题上工作了几天.不幸的是,该平台使我无法使用任何常用的有用工具来查找确切的问题.当在运行Linux的PC上运行相同的代码时,我没有遇到这样的错误.

在下面的示例中,我可以通过取消注释字符串,列表或矢量线来可靠地重现问题.让它们留下评论会导致应用程序运行完成.我希望有什么东西会破坏堆,但是我看不到什么?在发出分段错误之前,程序将运行几秒钟.

代码使用arm-linux交叉编译器编译:

arm-linux-g++ -Wall -otest fault.cpp -ldl -lpthread
arm-linux-strip test
Run Code Online (Sandbox Code Playgroud)

任何想法都非常感激.

#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( …
Run Code Online (Sandbox Code Playgroud)

c++ linux string arm segmentation-fault

5
推荐指数
1
解决办法
1716
查看次数