如何测试线程安全[重复]

时间:2022-02-17 21:00:39

This question already has an answer here:

这个问题在这里已有答案:

Do you have any advices how to test a multithreaded application?

您对如何测试多线程应用程序有什么建议吗?

I know, threading errors are very difficult to catch and they may occur at anytime - or not at all. Tests are difficult and the results are never for sure. Certainly it is best to carefully design and program the concurrent modules.
Nevertheless - I do not want to leave out the test aspect. So running a lot of threads that are all working on the same items can invoke threading errors, sometimes.

我知道,线程错误很难捕获,它们可能随时发生 - 或者根本不发生。测试很困难,结果永远不会确定。当然最好仔细设计和编程并发模块。尽管如此 - 我不想忽略测试方面。因此,运行大量同时处理相同项目的线程有时会调用线程错误。

Any ideas or best practices to get a high hit rate of hidden threading errors?
(I am using .Net/C#)

任何想法或最佳实践,以获得隐藏线程错误的高命中率? (我正在使用.Net / C#)

4 个解决方案

#1


You can use some good tools to test all the threading issues like Data races, deadlocks, stalled threads etc. intel-thread-checker is one such good tool.

您可以使用一些好的工具来测试所有线程问题,如数据争用,死锁,停滞线程等.intel-thread-checker就是这样一个很好的工具。

You can also try, CHESS by Microsoft Research

您也可以尝试使用Microsoft Research的CHESS

#2


Try increasing the number of threads to a large number if possible, even beyond how many will be used in a release. With lots of threads running your program, an error will appear more often since more threads are running over the code.

如果可能的话,尝试将线程数增加到一个很大的数量,甚至超过在发布中使用的数量。由于运行程序的线程很多,因此在代码上运行更多线程时会出现更多错误。

Double check your declarations, locks, unlocks, semaphore counts, etc and make sure they make sense.

仔细检查您的声明,锁定,解锁,信号量计数等,并确保它们有意义。

Create a test document or spreadsheet, and using your knowledge of the code, think about where possible race conditions or deadlocks could occur.

创建测试文档或电子表格,并使用您对代码的了解,考虑可能出现的竞争条件或死锁的位置。

Grab some people from the hall and do a 'hallway usability test' (Joel on Software said that I think?). Generally, people who have no idea what your program does/is about will be able to break it easily.

从大厅拿走一些人并进行“走廊可用性测试”(Joel on Software说我认为?)。一般来说,不知道你的程序是什么/关于什么的人将能够轻松地打破它。

#3


Good question. I usually test for race-conditions by spawning many threads and letting them wildly perform the operations which I suspect might be subject to race conditions.

好问题。我通常通过产生许多线程并让它们疯狂地执行我怀疑可能受到竞争条件影响的操作来测试竞争条件。

Maybe you can look at PNUnit - although it's probably a little different from what you are looking for. The authors say they built it because "we needed to simulate hundreds of clients against the same server".

也许你可以看看PNUnit - 虽然它可能与你想要的有点不同。作者说他们建造它是因为“我们需要针对同一台服务器模拟数百个客户端”。

#4


grep code for calls to threading routines. If any are found, fail the test, as your code has multi-threading bugs.

用于调用线程例程的grep代码。如果发现任何问题,则测试失败,因为您的代码存在多线程错误。

If it passes, expand your search to the parts of libraries you use, until it fails or (unlikely) is proven thread-safe (i.e. single-threaded).

如果它通过,将搜索扩展到您使用的库的部分,直到它失败或(不太可能)被证明是线程安全的(即单线程)。

Once you know you have threading bugs, the testing part of the job is done. All that remains is the small matter of finding and removing them...

一旦你知道你有线程错误,就完成了工作的测试部分。剩下的就是寻找和删除它们的小问题......

#1


You can use some good tools to test all the threading issues like Data races, deadlocks, stalled threads etc. intel-thread-checker is one such good tool.

您可以使用一些好的工具来测试所有线程问题,如数据争用,死锁,停滞线程等.intel-thread-checker就是这样一个很好的工具。

You can also try, CHESS by Microsoft Research

您也可以尝试使用Microsoft Research的CHESS

#2


Try increasing the number of threads to a large number if possible, even beyond how many will be used in a release. With lots of threads running your program, an error will appear more often since more threads are running over the code.

如果可能的话,尝试将线程数增加到一个很大的数量,甚至超过在发布中使用的数量。由于运行程序的线程很多,因此在代码上运行更多线程时会出现更多错误。

Double check your declarations, locks, unlocks, semaphore counts, etc and make sure they make sense.

仔细检查您的声明,锁定,解锁,信号量计数等,并确保它们有意义。

Create a test document or spreadsheet, and using your knowledge of the code, think about where possible race conditions or deadlocks could occur.

创建测试文档或电子表格,并使用您对代码的了解,考虑可能出现的竞争条件或死锁的位置。

Grab some people from the hall and do a 'hallway usability test' (Joel on Software said that I think?). Generally, people who have no idea what your program does/is about will be able to break it easily.

从大厅拿走一些人并进行“走廊可用性测试”(Joel on Software说我认为?)。一般来说,不知道你的程序是什么/关于什么的人将能够轻松地打破它。

#3


Good question. I usually test for race-conditions by spawning many threads and letting them wildly perform the operations which I suspect might be subject to race conditions.

好问题。我通常通过产生许多线程并让它们疯狂地执行我怀疑可能受到竞争条件影响的操作来测试竞争条件。

Maybe you can look at PNUnit - although it's probably a little different from what you are looking for. The authors say they built it because "we needed to simulate hundreds of clients against the same server".

也许你可以看看PNUnit - 虽然它可能与你想要的有点不同。作者说他们建造它是因为“我们需要针对同一台服务器模拟数百个客户端”。

#4


grep code for calls to threading routines. If any are found, fail the test, as your code has multi-threading bugs.

用于调用线程例程的grep代码。如果发现任何问题,则测试失败,因为您的代码存在多线程错误。

If it passes, expand your search to the parts of libraries you use, until it fails or (unlikely) is proven thread-safe (i.e. single-threaded).

如果它通过,将搜索扩展到您使用的库的部分,直到它失败或(不太可能)被证明是线程安全的(即单线程)。

Once you know you have threading bugs, the testing part of the job is done. All that remains is the small matter of finding and removing them...

一旦你知道你有线程错误,就完成了工作的测试部分。剩下的就是寻找和删除它们的小问题......