Can anyone point me at the thing I try to do in this code, because SecondLoop
thread is unreachable at all? It becomes reachable only if I remove while(true)
loop.
#include <iostream>
#include <thread>
using namespace std;
void Loop() {
while(true) {
(do something)
}
}
void SecondLoop() {
while(true) {
(do something)
}
}
int main() {
thread t1(Loop);
t1.join();
thread t2(SecondLoop);
t2.join(); // THIS THREAD IS UNREACHABLE AT ALL!
return false;
}
The reason why I use multithreading is because I need to get two loops running at the same time.
Based on my comment and what @Nidhoegger answered I suggest:
}
To run
Loop
andSecondLoop
concurrency, you have to do something like:as
join
block current thread to wait the other thread finishes.join
blocks the current thread to wait for another thread to finish. Since yourt1
never finishes, your main thread waits for it indefinitely.Edit:
To run two threads indefinitely and concurrency, first create the threads, and then wait for both:
.join() waits for the thread to end (so in this case if you break out of the while loops and exit the thread function)
using while(true) is linked to the tread running , you should look for a way to exit that loop, use some sort of loop control