因为在做的工程项目里使用了Qt,而实际上不涉及到屏幕显示,工程代码里使用了QThread,且没有使用Qt核心的信号与槽,为了以后移植准备使用更加通用的C++11 stl中的thread取代QThread。

  下面是一些测试过程,因为没有为这个小测试建一个版本控制,所以为了能记录每步测试修改,这里将编写代码编写博客,开始吧。

 1 #include <iostream>
 2 #include <thread>
 3 #include <chrono>
 4 #include <functional>
 5 
 6 using namespace std;
 7 
 8 class MyThread
 9 {
10 public:
11     MyThread()
12     {
13         thread t([this]()-> void {
14             
15             run();
16 
17         });
18         t.detach();
19     }
20 
21 public:
22     void run()
23     {
24         for(int i=0;i<5;i++)
25         {
26                     cout<<"Hello Nelson!"<<endl;
27             std::this_thread::sleep_for(std::chrono::milliseconds(1000));
28         }
29     }
30 };
31 
32 int main(int argc,char *argv[])
33 {
34 
35     MyThread mythread;
36 
37     cout<<"main thread is over"<<endl;
38 
39     return 0;
40 }
View Code

相关文章: