简体   繁体   English

使用 eclipse cdt 调试多线程应用程序

[英]Debugging multithread application with eclipse cdt

I am developing multithread application with c++ using eclipse cdt.我正在使用 eclipse cdt 开发 c++ 的多线程应用程序。 I need to debug my code, i have tried to write to console by using printf but it is not very useful.我需要调试我的代码,我尝试使用 printf 写入控制台,但它不是很有用。 How do i debug my multithread code?我如何调试我的多线程代码?

It depends on what you're looking for, but debugging multithreaded applications can be very similar or extremely dissimilar to debugging single threaded apps.这取决于您要查找的内容,但调试多线程应用程序可能与调试单线程应用程序非常相似或极为不同。

First, some example methods you can still use:首先,您仍然可以使用一些示例方法:

  1. Use breakpoints with an IDE使用带有 IDE 的断点
  2. Use various solid relevant debugging tools such as Valgrind.使用Valgrind等各种可靠的相关调试工具。 Here's a link to a good article full of tools:C++ TUTORIAL MULTI-THREADED PROGRAMMING DEBUGGING - 2020这是一篇包含大量工具的好文章的链接:C++ 教程多线程编程调试 - 2020
  3. Output to separate files OR use locks if outputting to the same file/stream (not recommended but will touch on this) Output 分隔文件或如果输出到相同的文件/流则使用锁(不推荐但会触及)

I assume the issue you're running into here with printf is that it's like multiple threads writing to the same file- it's a race condition for what output you'll see on the screen.我假设您在这里遇到的问题 printf 就像多个线程写入同一个文件一样 - 这是您将在屏幕上看到的 output 的竞争条件。 I'd personally still recommend using a tool like valgrind if you're running into a multithreaded error, but if you'd like to continue using simple console output, try...如果您遇到多线程错误,我个人仍然建议使用像 valgrind 这样的工具,但如果您想继续使用简单的控制台 output,请尝试...

  • Write to different files instead of to standard output/the console写入不同的文件而不是标准输出/控制台
  • Use a lock/mutex to assure only 1 thread is writing to standard output at the same time (which could mask race condition or such issues due to adding in blocking for that lock before printing)使用锁/互斥锁确保只有 1 个线程同时写入标准 output(这可能会掩盖竞争条件或由于在打印前为该锁添加阻塞而导致的此类问题)
  • A more so wild idea: Share state (via a lock to avoid multiple threads reading or writing to the data at the same time) or otherwise return output to a single thread that's dedicated to writing out your debug info.一个更疯狂的想法:共享 state(通过锁以避免多个线程同时读取或写入数据)或以其他方式将 output 返回到一个专用于写出调试信息的线程。 Similar to the above but slightly different in implementation and useful if you'd like to design in a dedicated multithreaded log system for future improvements与上面类似,但在实现上略有不同,如果您想在专用的多线程日志系统中设计以供将来改进,则很有用

All in all, the exact tool you should be using here depends on the exact issue you're facing and thus no strong recommendations just yet.总而言之,您应该在此处使用的确切工具取决于您面临的具体问题,因此目前还没有强烈的建议。 Please apply the tools above (including breakpoints in Eclipse) to suit your situation.请根据您的情况应用上述工具(包括 Eclipse 中的断点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM