Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed start of a thread in C++ 11

I'm getting into C++11 threads and have run into a problem.

I want to declare a thread variable as global and start it later.

However all the examples I've seen seem to start the thread immediately for example

thread t(doSomething); 

What I want is

thread t; 

and start the thread later.

What I've tried is

if(!isThreadRunning) {     thread t(readTable); } 

but now t is block scope. So I want to declare t and then start the thread later so that t is accessible to other functions.

Thanks for any help.

like image 869
Smithy Avatar asked Aug 27 '14 10:08

Smithy


People also ask

How do you make a thread wait in C++?

To wait for a thread use the std::thread::join() function. This function makes the current thread wait until the thread identified by *this has finished executing. A C++ program is given below.

How do you check if a thread is running or not C++?

if (status == std::future_status::ready) { std::cout << "Thread finished" << std::endl; } else { std::cout << "Thread still running" << std::endl; } t. join(); // Join thread. } This is of course because the thread status is checked before the task is finished.

Do you have to join threads in C?

no, you can detach one thread if you want it to leave it alone. If you start a thread, either you detach it or you join it before the program ends, otherwise this is undefined behaviour.


2 Answers

std::thread's default constructor instantiates a std::thread without starting or representing any actual thread.

std::thread t; 

The assignment operator moves the state of a thread object, and sets the assigned-from thread object to its default-initialized state:

t = std::thread(/* new thread code goes here */); 

This first constructs a temporary thread object representing a new thread, transfers the new thread representation into the existing thread object that has a default state, and sets the temporary thread object's state to the default state that does not represent any running thread. Then the temporary thread object is destroyed, doing nothing.

Here's an example:

#include <iostream> #include <thread>  void thread_func(const int i) {     std::cout << "hello from thread: " << i << std::endl; }  int main() {     std::thread t;     std::cout << "t exists" << std::endl;      t = std::thread{ thread_func, 7 };     t.join();      std::cout << "done!" << std::endl; } 
like image 61
Sam Varshavchik Avatar answered Sep 28 '22 17:09

Sam Varshavchik


There is no "standard" of creating a thread "suspended" which I assume is what you wanted to do with the C++ thread library. Because it is not supported on every platform that has threads, it is not there in the C++ API.

  1. You might want to create a class with all the data it is required but not actually run your thread function. This is not the same as creating the thread but may be what you want. If so, create that, then later bind the object and its operator() or start() function or whatever to the thread.

  2. You might want the thread id for your thread. That means you do actually need to start the thread function. However it can start by waiting on a condition variable. You then signal or broadcast to that condition variable later when you want it to continue running. Of course you can have the function check a condition after it resumes in case you might have decided to close it and not run it after all (in which case it will just return instantly).

  3. You might want a std::thread object with no function. You can do that and attach it to a function later to run that function in a new thread.

like image 32
CashCow Avatar answered Sep 28 '22 15:09

CashCow