谷歌测试中的使用线程

Usage threads in google tests

本文关键字:线程 测试 谷歌      更新时间:2023-10-16

我想创建一些测试来检查应用程序中的多线程。我使用谷歌测试框架。我的以下代码未使用错误消息进行编译error: invalid use of non-static member function

TEST_F( tc, t ) {
  std::thread thread1 ( f1, p1 );
  std::thread thread2 ( f2, p2 );
  thread1.join();
  thread2.join();    
}

我使用 GCC 5.2.1 进行编译。

你能指出我热解决它吗?

你需要告诉 std::threadf1 和 f2 是你的测试夹具的方法,使用 std::bind

TEST_F( tc, t ) {
    std::thread thread1(std::bind(&tc::f1, this, p1));
    std::thread thread2(std::bind(&tc::f2, this, p2));
    thread1.join();
    thread2.join();    
}