Try...catch 在具有 posix 线程的嵌入式 ARM 上导致分段错误
今天,我发布了一个有关 std::string 销毁后分段错误的问题(请参阅 这篇文章)。我已经剥离了代码,以便不使用 STL,但有时仍然会出现分段错误。
以下代码在我运行 Linux 的 PC 上运行良好。但是使用我们的嵌入式设备制造商提供的 ARM 交叉编译器,它会在 catch (...)
之前给出分段错误。
此问题似乎与 Google 网上论坛中的这篇文章,但我还没有找到任何解决方案。
代码是使用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);
}
Today, I posted a problem about a segmentation fault after destruction of a std::string (see this post). I've stripped the code so that I don't use the STL and still have a segmentation fault sometimes.
The following code works just fine on my PC running Linux. But using the ARM crosscompiler suppplied by the manufactor of our embedded device, it gives a segmentation fault just before catch (...)
.
This problems seems to have a link with this post in Google Groups, but I haven't found any solution yet.
The code is compiled using an ARM cross compiler
Any suggestions are still welcome!
#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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我曾经遇到过这样的问题,这是由于链接到没有线程支持的 libstdc++ 版本而引起的,这意味着所有线程共享一个公共异常处理堆栈,从而带来灾难性的后果。
确保使用
--enable-threads=posix
配置交叉编译器及其库。I once encountered a problem like this which was caused by linking with a version of
libstdc++
with no threading support, meaning that all threads shared a common exception handling stack with disastrous consequences.Make sure the cross-compiler and its libraries were configured with
--enable-threads=posix
.只是一个诊断问题:如果不在 Start() 中分离线程会发生什么?您必须将线程 pthread_join() 返回到 main() 中。
另外,您是否考虑过 Boost 的线程?这可能更合适,因为您使用的是 C++ 而不是 C。
Just a diagnostic question: What happens if you don't detach the thread in Start()? You'd have to pthread_join() the threads back in main().
Also, have you considered Boost's threads? That might be more appropriate since you're using C++ rather than C.