I'm working on a project where I want to create a thread that branches off the main thread, but does not block it until returned. What I mean by this is that I need the main thread to create the child thread, and the main thread keep working while the child thread goes its separate way (branching off).
I've recreated a smaller-scale version of my code, for simplicity's sake:
#include <iostream>
#include <thread>
using namespace std;
void wait() {
std::this_thread::sleep_for(std::chrono::duration<int, std::ratio<1,1000>>(200));
}
void one() {
static int x = 0;
while(true) {
cout << "one" << endl;
wait();
}
}
void two() {
while(true) {
cout << "two" << endl;
wait();
}
}
int main() {
thread t1(one);
thread t2(two);
t1.join();
t2.join();
cout << "Do stuff beyond here..";
while(true) {
cout << "three" << endl;
wait();
}
return 0;
}
My problem is that the threes don't start showing up until after the ones and twos have finished.. Is there any way around this? I've tried omitting the calls to join, but that only crashes the program.
Any help would be greatly appreciated.