【发布时间】:2010-03-09 21:22:16
【问题描述】:
我在嵌入式 Arm Linux 平台上运行的应用程序出现问题已经工作了几天。不幸的是,该平台使我无法使用任何常用的有用工具来查找确切的问题。在运行 Linux 的 PC 上运行相同的代码时,我没有收到此类错误。
在下面的示例中,我可以通过取消注释字符串、列表或向量行来可靠地重现问题。让他们评论会导致应用程序运行完成。我预计某些东西正在破坏堆,但我看不到什么?该程序将运行几秒钟,然后给出分段错误。
代码使用arm-linux交叉编译器编译:
arm-linux-g++ -Wall -otest fault.cpp -ldl -lpthread
arm-linux-strip test
非常感谢任何想法。
#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 );
}
【问题讨论】:
-
你检查过 std::string 在平台上没有 pthreads 的情况下工作吗?
-
并尝试了 2 个线程而不是 100 个线程?
-
是的,线程过多可能是导致 seg 错误的原因。
-
感谢大家的回复。是的,我尝试了几个线程。显示问题需要更长的时间,但仍然会发生。 std::string 在平台上确实有效(据我所知)。静态互斥锁会导致任何问题吗?我看不出任何其他变量如何导致问题,因为它们被保留在静态函数的范围内。
-
@Brad:你错过了
gcc的-pthread标志吗?
标签: c++ linux string segmentation-fault arm