【问题标题】:Usage threads in google tests谷歌测试中的使用线程
【发布时间】:2016-01-15 09:23:19
【问题描述】:

我想创建一些测试来检查我的应用程序中的多线程。我使用谷歌测试框架。我的以下代码未编译错误消息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 进行编译。

能否请您指出我要修复它?

【问题讨论】:

  • 什么是 f1、p1、f2、p2?
  • @ForEveR,一些简单的函数及其参数。在单线程测试中,我称它们为 f1(p1)
  • 您的警告消息表明 f1f2 是成员函数。这就是发生此错误的原因。

标签: c++ multithreading gcc googletest stdthread


【解决方案1】:

您需要使用std::bind 告诉 std::thread f1 和 f2 是您的测试夹具的方法:

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();    
}

【讨论】:

    猜你喜欢
    • 2022-07-27
    • 2012-03-30
    • 2020-09-03
    • 2023-03-11
    • 1970-01-01
    • 2015-03-27
    • 2023-02-17
    • 2012-08-17
    • 2014-04-04
    相关资源
    最近更新 更多