尝试在编译时查找线程问题有什么意义吗?
我说的是数据竞争、死锁、损坏的状态等......
最佳答案
虽然这不是编译时间,但您可能需要查看 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:
- Misuses of the POSIX pthreads API.
- Potential deadlocks arising from lock ordering problems.
- 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/