I am trying to use threading in an if statement.
The following code gives an error saying t1 in t1.join is not declared in this scope. Is there a way to make the loop skip the first thread operation and start multithreading for the other passes?
#include <thread>
void someFunction() {/*someTask*/}
void main() {
bool isFirstRun = true;
while(true){
if(isFirstRun==false){std::thread t1(someFunction);}
//some long task
if(isFirstRun==false){t1.join}
if(isFirstRun){isFirstRun = false;}
}
}
More generally, is there a way to create a thread like a global variable in C++ and control its execution anywhere in the code? I saw the following in java and thought this might solve my problem if implemented in C++:
Thread t1 = new Thread(new EventThread("e1"));
//some tasks here
t1.start();
//some tasks here
t1.join();