简体   繁体   English

如何使用线程进行Google测试

[英]How to use Google test with threads

I have a serial code which I want to test for thread safety. 我有一个序列代码,我想测试线程安全性。 I am using Google testing framework for my unit tests. 我正在使用Google测试框架进行单元测试。 How can I call those unit tests with multiple threads to test if the functions are safe to be called from multiple threads? 如何通过多个线程调用这些单元测试来测试是否可以安全地从多个线程调用这些函数?

Google Test doesn't have any specific feature to test for thread safety. Google Test没有任何特定功能来测试线程安全性。

You can always roll out your own threads running over your code concurrently and after joining them, check for some data to have a consistent value. 您始终可以同时在代码上运行自己的线程,并在加入它们之后,检查某些数据是否具有一致的值。

As an example, I have tested a thread-safe queue by running two threads for producer and consumer, where the producer inserts a fixed amount of elements in the queue and the consumer is set to extract the same number of elements. 例如,我通过为生产者和消费者运行两个线程来测试线程安全队列,其中生产者在队列中插入固定数量的元素,并且将消费者设置为提取相同数量的元素。 When both threads are done, I check for the queue to be empty, and the consumer to hold the list of elements in the right order. 当两个线程完成时,我检查队列是否为空,并且消费者以正确的顺序保存元素列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM