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