Why can't I declare a thread in an if in C++? Is there a way around? : <p>I am trying to use threading in an if statement. </p>

<p>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?</p>

<pre><code>  #include &lt;thread&gt;
  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;}
     }
 }
</code></pre>

<p>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++:</p>

<pre><code>Thread t1 = new Thread(new EventThread("e1")); 
//some tasks here
t1.start(); 
//some tasks here
t1.join();
</code></pre>
