【发布时间】:2011-04-26 23:56:28
【问题描述】:
今天,我发布了一个关于 std::string 销毁后出现分段错误的问题(请参阅this post)。我已经剥离了代码,因此我不使用 STL,并且有时仍然会出现分段错误。
以下代码在我运行 Linux 的 PC 上运行良好。但是使用我们的嵌入式设备制造商提供的 ARM 交叉编译器,它会在 catch (...) 之前出现分段错误。
这个问题在Google Groups中好像有this post的链接,但是我还没有找到解决办法。
代码是使用 ARM 交叉编译器编译的
仍然欢迎任何建议!
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *ExecuteThreadMethod(void *AThread);
class Thread
{
private:
pthread_t internalThread;
public:
void RunSigSegv()
{
try
{
for (int i = 0; i < 200; i++)
{
usleep(10000);
}
} // <---- Segmentation fault occurs here
catch(...)
{
}
}
void Start()
{
pthread_attr_t _attr;
pthread_attr_init(&_attr);
pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_DETACHED);
pthread_create (&internalThread, &_attr, ExecuteThreadMethod, this);
}
};
void *ExecuteThreadMethod(void *AThread)
{
((Thread *)AThread)->RunSigSegv();
pthread_exit(NULL);
}
Thread _thread1;
Thread _thread2;
Thread _thread3;
Thread _thread4;
void s()
{
_thread1.Start();
_thread2.Start();
_thread3.Start();
_thread4.Start();
}
int main(void)
{
s();
usleep(5000000);
}
【问题讨论】:
标签: linux multithreading gcc posix arm