如何证明多线程工作? [英] How to prove that multithreading is working?

查看:128
本文介绍了如何证明多线程工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能证明多线程在我的C#程序工作吗?这为测试需求。例如,我将不得不在一定的锁定添加到一个记录器类(是的,我知道,我不应该写我自己的日志类),我需要一个测试案例来证明的变化会工作。

How can I prove that multithreading is working in my C# programs? This is for a testing requirement. For example, I'm going to have to add some locking to a logger class (yes I know, I shouldn't have written my own logging class), and I need a test case to prove that the change will work.

推荐答案

如果你想测试你的锁定代码正确同步到您的日志(S)的访问,你需要建立能保证测试争。这可能需要重构你的代码,这样你可以注入一个模拟日志写类,可容纳日志对任意时间周期内锁定。

If you want to test that your locking code is correctly synchronizing access to your log(s), you need to construct tests that guarantee contention. This may require you to refactor your code so that you can inject a mock log writer class that can hold the log's lock for arbitrary periods of time.

这是一个广泛的话题,你可以找到在计算器上几个相关的问题,这些问题都值得一读:

This is a broad topic, and you can find several related questions on StackOverflow, which are all worth reading:

  • How do I perform a Unit Test using threads?
  • How to write an automated test for thread safety
  • What are some strategies to unit test a scheduler?
  • Unit testing a multithreaded application?
  • Should I unit test for multithreading problems before writing any lock? (.NET C# TDD)

CHESS 是确定下一个发展框架的断言违法行为,死锁,活锁,数据种族和存储模型的错误。我还没有实际使用,但它看起来像它可能是非常有帮助的。

CHESS is a framework under development for identifying "assertion violations, deadlocks, livelocks, data-races, and memory-model errors." I haven't actually used this, but it looks like it might be extremely helpful.

这篇关于如何证明多线程工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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