线程安全测试 [英] Thread Safety Testing

查看:32
本文介绍了线程安全测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 googles 的 gtest 来编写我的单元测试,但它看起来不能测试线程安全性(即,从多个线程访问某些内容并确保其行为符合规范).

I currently use googles' gtest to write my unit tests, but it doesn't look like it can test thread-safety (that is, accessing something from multiple threads and ensuring it behaves according to spec).

你用什么来测试线程安全?我想要一些跨平台的东西,但是,它至少必须在 Windows 上运行.

What do you use to test thread safety? I'd like something cross-platform, but, it definitely has to work on Windows atleast.

谢谢!

推荐答案

如果多线程代码不是立即、显然、可证明是正确的,那么它几乎肯定是错误的.如果是,则无需对其进行测试.

If multithreaded code isn't immediately, obviously, provably correct then it is almost certainly wrong. And if it is, you don't need to test it.

说真的:共享可变状态应该是非常本地化和罕见的,并且这样做的类应该是正确的.

Seriously: shared mutable state should be extremely localised and rare, and the classes that do it should be demonstrably correct.

您的线程通常应该通过安全原语(例如线程安全工作队列)进行交互.如果您的代码周围散布着大量数据结构,每个数据结构都有自己的锁定策略,那么您的代码几乎肯定会包含死锁和竞争条件.大量的测试工作只会发现一些问题.

Your threads should normally interact via safe primitives (eg a thread-safe work queue). If you have lots of data structures scattered around your code each with its own locking strategy then your code almost certainly contains deadlocks and race conditions. A big testing effort will only find some of the problems.

这篇关于线程安全测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆