在c++11当中,要是想用线程相关的东西,需要引用新的头文件。举个helloworld的栗子:
点击(此处)折叠或打开
-
#include <iostream>
-
#include <thread>
-
-
void func(){
-
std::cout<<"Hello world~"<<std::endl;
-
}
-
int main(int argc, char** argv) {
-
std::thread t(func);
-
t.join();
-
return 0;
- }
点击(此处)折叠或打开
-
#include <iostream>
-
#include <thread>
-
-
class task{
-
public:
-
void operator()() const{
-
do_func1();
-
do_func2();
-
}
-
void do_func1()const{ std::cout<<"func1"<<std::endl;}
-
void do_func2()const{ std::cout<<"func2"<<std::endl;}
-
};
-
int main(int argc, char** argv) {
-
task f;
-
std::thread my_thread(f);
-
my_thread.join();
-
return 0;
- }