I've got 2 class' where in the constructor of each spawns a thread which just prints hello world or goodbye universe. My goal is for the program to print out both hello world and goodbye universe at the same time. Problem is the program currently waits for the first thread to finish before starting the second. Its basically thread 1 is blocking the creation of threa2 until it finishes. What is the correct way for both threads to be executing at the same time?
My code is
#include <iostream>
#include "voltage.h"
#include <thread>
class MyClass final
{
private:
std::thread mythread;
void _ThreadMain()
{
int x = 1000;
while(x>0)
{
std::cout << "hello world " << x << std::endl;
x--;
}
};
public:
MyClass()
: mythread{}
{
mythread = std::thread{&MyClass::_ThreadMain, this};
mythread.join();
}
};
class MyClass2 final
{
private:
std::thread mythread;
void _ThreadMain()
{
int x = 1000;
while(x>0)
{
std::cout << "goodbye universe " << x << std::endl;
x--;
}
};
public:
MyClass2()
: mythread{}
{
mythread = std::thread{&MyClass2::_ThreadMain, this};
mythread.join();
}
};
int main(int argc, char *argv[])
{
MyClass *myClass = new MyClass();
MyClass2 *myClass2 = new MyClass2();
return 0;
}
My compile arguments are
g++ -g -march=armv6 -marm -I Sources/ main.cpp -L libraries/ -lyocto-static -lm -lpthread -lusb-1.0
Most of that is for other parts of the program i'm working on