c++ - 查找线程错误

标签 c++ multithreading

尝试在编译时查找线程问题有什么意义吗?

我说的是数据竞争、死锁、损坏的状态等......

最佳答案

虽然这不是编译时间,但您可能需要查看 Helgrind :

Overview

Helgrind is a Valgrind tool for detecting synchronisation errors in C, C++ and Fortran programs that use the POSIX pthreads threading primitives.

The main abstractions in POSIX pthreads are: a set of threads sharing a common address space, thread creation, thread joining, thread exit, mutexes (locks), condition variables (inter-thread event notifications), reader-writer locks, spinlocks, semaphores and barriers.

Helgrind can detect three classes of errors, which are discussed in detail in the next three sections:

  1. Misuses of the POSIX pthreads API.
  2. Potential deadlocks arising from lock ordering problems.
  3. Data races -- accessing memory without adequate locking or synchronisation.

Problems like these often result in unreproducible, timing-dependent crashes, deadlocks and other misbehaviour, and can be difficult to find by other means.

Helgrind is aware of all the pthread abstractions and tracks their effects as accurately as it can. On x86 and amd64 platforms, it understands and partially handles implicit locking arising from the use of the LOCK instruction prefix.

Helgrind works best when your application uses only the POSIX pthreads API. However, if you want to use custom threading primitives, you can describe their behaviour to Helgrind using the ANNOTATE_* macros defined in helgrind.h. This functionality was added in release 3.5.0 of Valgrind, and is considered experimental.

由于 Boost.Threads 基于 POSIX pthreads(至少在 Linux 上),我猜它也适用于它。

关于c++ - 查找线程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3099794/

相关文章:

c++ - 陷入循环等待状态。需要帮助才能出去

python - Python Thread()删除换行符?

c# - 获取异步结果死锁(尽管将 configure await 设置为 false)

c++ - 如何循环遍历 map 的 C++ map ?

c++ - 取消引用返回的引用

c++ - 尝试在构造函数中设置成员变量时代码中断

java - java中如何停止一个线程

c++ - 为什么用户定义的字符串文字和整数文字具有不同的行为?

c++ - 为什么 C++ 没有计算函数参数的特定顺序?

c# - 如何以随机间隔将执行挂起一段随机时间