C++多线程
目录
5、网络编程&多线程的讲解
6、Qt&Socket网络编程
1、简单多线程实例(使用join())
使用thread创建子线程,并立马使用jion()阻塞住,直到子线程执行完毕;(两个子线程并行执行,join函数会阻塞主流程,所以子线程都执行完成之后才继续执行主线程)代码如下:
1 #include <iostream> 2 #include <thread> 3 #include <Windows.h> 4 5 using namespace std; 6 7 void thread01() 8 { 9 cout << "Thread 01 is working !" << endl; 10 Sleep(100); 11 cout << "Thread 01 is sleeping over !" << endl; 12 } 13 void thread02() 14 { 15 cout << "Thread 02 is working !" << endl; 16 Sleep(100); 17 cout << "Thread 02 is sleeping over !" << endl; 18 } 19 20 int main() 21 { 22 thread task01(thread01); //创建子线程并执行该子线程 23 thread task02(thread02); 24 task01.join(); //等待线程task01执行完毕之后,再去执行下一句 25 task02.join(); //等待线程task02执行完毕之后,再去执行下一句 26 for (int i = 0; i < 2; i++) 27 { 28 cout << "Main thread is working !" << endl; 29 Sleep(200); 30 } 31 32 system("pause"); 33 }
执行结果:
执行第二遍:
2、简单多线程实例---使用detach()
detach将子线程从主流程中分离,独立运行,不会阻塞主线程:
1 #include <iostream> 2 #include <thread> 3 #include <Windows.h> 4 5 using namespace std; 6 7 void thread01() 8 { 9 cout << "Thread 01 is working !" << endl; 10 Sleep(100); 11 cout << "Thread 01 is sleeping over !" << endl; 12 } 13 void thread02() 14 { 15 cout << "Thread 02 is working !" << endl; 16 Sleep(100); 17 cout << "Thread 02 is sleeping over !" << endl; 18 } 19 20 int main() 21 { 22 thread task01(thread01); //创建子线程并执行该子线程 23 thread task02(thread02); 24 task01.detach(); //等待线程task01执行完毕之后,再去执行下一句 25 task02.detach(); //等待线程task02执行完毕之后,再去执行下一句 26 for (int i = 0; i < 2; i++) 27 { 28 cout << "Main thread is working !" << endl; 29 Sleep(200); 30 } 31 32 system("pause"); 33 }
3、带参子线程
1 #include <iostream> 2 #include <thread> 3 #include <Windows.h> 4 5 using namespace std; 6 7 void thread01(int num) 8 { 9 cout << "Thread 01 is working !" << endl; 10 cout << "Thread01's sleeping time is " << num << endl; 11 Sleep(100); 12 cout << "Thread 01 is sleeping over !" << endl; 13 } 14 void thread02(int num) 15 { 16 cout << "Thread 02 is working !" << endl; 17 cout << "Thread02's sleeping time is " << num << endl; 18 Sleep(300); 19 cout << "Thread 02 is sleeping over !" << endl; 20 } 21 22 int main() 23 { 24 thread task01(thread01,100); //创建子线程并执行该子线程 25 thread task02(thread02,300); 26 task01.join(); //等待线程task01执行完毕之后,再去执行下一句 27 task02.join(); //等待线程task02执行完毕之后,再去执行下一句 28 for (int i = 0; i < 2; i++) 29 { 30 cout << "Main thread is working !" << endl; 31 Sleep(200); 32 } 33 34 system("pause"); 35 }
4、多线程竞争数据,使用mutex阻止多线程之间数据的竞争(互斥量)
因为在线程1和线程2只均改变了全局变量totalNum
为了在使多线程之间相互影响,需要声明一个互斥对象保持数据同步
互斥量技术从字面也可以理解,就是临界区有线程访问,其它线程就得排队等待,它们的访问是互斥的,实现方式就是给临界区加锁与释放锁。
1 #include <iostream> 2 #include <thread> 3 #include <Windows.h> 4 #include <mutex> 5 6 using namespace std; 7 int totalNum = 100; 8 9 /* 10 因为在线程1和线程2只均改变了全局变量totalNum 11 为了在使多线程之间相互影响,需要声明一个互斥对象保持数据同步 12 */ 13 std::mutex mu; //声明线程互斥对象 14 15 void thread01(int num) 16 { 17 mu.lock(); //同步数据锁 18 cout << totalNum << endl; 19 totalNum--; 20 Sleep(100); 21 mu.unlock(); //解除锁定 22 } 23 void thread02(int num) 24 { 25 mu.lock(); //同步数据锁 26 cout << totalNum << endl; 27 totalNum--; 28 Sleep(300); 29 mu.unlock(); //解除锁定 30 } 31 32 int main() 33 { 34 thread task01(thread01,100); //创建子线程并执行该子线程 35 thread task02(thread02,300); 36 task01.join(); //等待线程task01执行完毕之后,再去执行下一句 37 task02.join(); //等待线程task02执行完毕之后,再去执行下一句 38 for (int i = 0; i < 2; i++) 39 { 40 cout << "Main thread is working !" << endl; 41 Sleep(200); 42 } 43 44 system("pause"); 45 }
上面代码中,子函数Tread01()和Tread02()中的语句:totalNum--;即临界区。
不同线程之间相互有数据干扰的地方就是临界区。(或引起问题的地方)
5、网络编程&多线程的讲解
参考博客:
https://blog.csdn.net/qq_42564846/article/details/82736100 参考了《TCP/IP网络编程 ---尹圣雨》
6、Qt&Socket网络编程
参考博客:
https://blog.csdn.net/u014252478/article/details/80377103