In this section:
Start a thread
Each program will have at least one thread, main function is to perform the entrance, which we call the main thread, and the rest have their own sub-thread entry function, the main thread and the child threads to run simultaneously. Child thread starts when std :: thread object is created.
1. None None Return Value parameter function do child thread inlet:
void func() { cout << "func" <<endl; } std::thread my_job(func);
2. Overload Class () operator as a child thread inlet:
class thread_test { public: void oprator()() const { cout << "thread test" <<endl; } }; thread_test t; std::thread my_job(t);
3. Use lamda expression as the child thread entrance:
std::thread my_job([]{ cout << "lamda" <<endl; });
4. Common parameters, as a function of inlet sub-thread
void func(int i) { cout << "i = " << i << endl; } int count = 0; std::thread my_job(func, count);
The non-parametric class member functions as a sub-thread inlet
class thread_test { public: void func() { cout << "thread_test func" << endl; } }; thread_test t; std::thread my_job(&thread_test::func, &t);
6. The class has member functions as a reference sub-thread inlet
class thread_test { public: void func(int i) { cout << "thread_test func i = " << i << endl; } }; thread_test t; int count = 100; std::thread my_job(&thread_test::func, &t, count);