仅在多核上运行时出现分段错误
我正在使用一个 C++ 库,它是多线程的,并且可以使用变量设置工作线程的数量。该库使用 pthread。当我在使用 3 个或更多线程的四核计算机上运行作为库测试提供的应用程序时,就会出现问题。应用程序退出并出现分段错误运行时错误。当我尝试在库的某些部分插入一些跟踪“cout”时,问题得到解决,应用程序正常完成。 当在单核机器上运行时,无论使用多少个线程,应用程序都会正常完成。
我怎样才能找出问题出在哪里?
这是一种同步错误吗?我怎样才能找到它?有什么工具可以用来检查代码吗?
I am using a c++ library that is meant to be multi-threaded and the number of working threads can be set using a variable. The library uses pthreads. The problem appears when I run the application ,that is provided as a test of library, on a quad-core machine using 3 threads or more. The application exits with a segmentation fault runtime error. When I try to insert some tracing "cout"s in some parts of library, the problem is solved and application finishes normally.
When running on single-core machine, no matter what number of threads are used, the application finishes normally.
How can I figure out where the problem seam from?
Is it a kind of synchronization error? how can I find it? is there any tool I can use too check the code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你正在使用Linux(你提到了pthreads)。您是否考虑过运行valgrind?
Valgrind 具有用于检查数据竞争条件 (helgrind) 和内存问题 (memcheck) 的工具。 Valgrind 可能是在调试模式下发现这样的错误而不需要产生发布模式产生的崩溃。
Sounds like you're using Linux (you mention pthreads). Have you considered running valgrind?
Valgrind has tools for checking for data race conditions (helgrind) and memory problems (memcheck). Valgrind may be to find such an error in debug mode without needing to produce the crash that release mode produces.
一些一般性的调试建议。
Some general debugging recommendations.
您正在陷入竞争状态。
多个线程在同一资源上交互。
可能的罪魁祸首有很多,但如果没有消息来源,我们所说的任何事情都只是猜测。
您想要创建一个核心文件;然后使用核心文件调试应用程序。这会将调试器设置为应用程序崩溃时的状态。这将允许您检查变量/寄存器等。
如何执行此操作很大程度上取决于您的系统。
快速谷歌揭示了这一点:
http://www.codeguru。 com/forum/archive/index.php/t-299035.html
希望这有帮助。
You are running into a race condition.
Where multiple threads are interacting on the same resource.
There are a whole host of possible culprits, but without the source anything we say is a guess.
You want to create a core file; then debug the application with the core file. This will set up the debugger to the state of the application at the point it crashed. This will allow you to examin the variables/registers etc.
How to do this will very depending on your system.
A quick Google revealed this:
http://www.codeguru.com/forum/archive/index.php/t-299035.html
Hope this helps.