简体   繁体   中英

gcov not working with pthreads, WSL-2, Clion

Background: This seems to work fine if I don't use any threads or just spawn 1 thread, which makes this all the more confusing.

Clion Project here

Problem: I set up a basic example project that starts 2 threads and does some printing to the console from main thread, thread 2, and thread 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;
}

Compiling using:

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

When I run in clion using "Run 'EvalTest' with Coverage", I get the following error:

Could not find code coverage data

在此处输入图片说明

So it's not producing the gcov files needed, but it works fine if I comment out the following line of code:

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

在此处输入图片说明

Needed to do threadObj.join() and threadObj2.join(). So code looks like:

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

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