简体   繁体   中英

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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