gpt4 book ai didi

c++ - 查找线程错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:32:38 26 4
gpt4 key购买 nike

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

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

最佳答案

虽然这不是编译时间,但您可能需要查看 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com