繁体   English   中英

gcov 不适用于 pthreads、WSL-2、Clion

[英]gcov not working with pthreads, WSL-2, Clion

背景:如果我不使用任何线程或只生成 1 个线程,这似乎工作正常,这使得这一切更加混乱。

克利翁项目在这里

问题:我设置了一个基本示例项目,它启动 2 个线程并从主线程、线程 2 和线程 3 向控制台打印一些内容。

#include <iostream>
#include <thread>

void thread1()
{
    for(int i = 0; i < 10000; i++)
    {
        std::cout << "thread1" << std::endl;
    }
}

void thread2()
{
    for(int i = 0; i < 10000; i++)
    {
        std::cout << "thread2" << std::endl;
    }
}

int main()
{
    std::cout << "Hello, World!" << std::endl;
    std::thread threadObj(thread1);
    std::thread threadObj2(thread2);
    for(int i = 0; i < 10000; i++)
    {
        std::cout<<"MainThread"<<std::endl;
    }
    threadObj.join();
    std::cout<<"Exit of Main function"<<std::endl;
    return 0;
}

编译使用:

--coverage -pthread -g -std=gnu++2a

当我使用“Run 'EvalTest' with Coverage”在 clion 中运行时,出现以下错误:

找不到代码覆盖率数据

在此处输入图片说明

因此,它不会生成所需的 gcov 文件,但是如果我注释掉以下代码行,它就可以正常工作:

int main()
{
    std::cout << "Hello, World!" << std::endl;
    std::thread threadObj(thread1);
//    std::thread threadObj2(thread2);
    for(int i = 0; i < 10000; i++)
    {
        std::cout<<"MainThread"<<std::endl;
    }
    threadObj.join();
    std::cout<<"Exit of Main function"<<std::endl;
    return 0;
}

在此处输入图片说明

需要做 threadObj.join() 和 threadObj2.join()。 所以代码看起来像:

int main()
{
    std::cout << "Hello, World!" << std::endl;
    std::thread threadObj(thread1);
    std::thread threadObj2(thread2);
    for(int i = 0; i < 10000; i++)
    {
        std::cout<<"MainThread"<<std::endl;
    }
    threadObj.join();
    threadObj2.join();  // need to join both thread for gcov to work properly
    std::cout<<"Exit of Main function"<<std::endl;
    return 0;
}

暂无
暂无

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

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