首页 / C++ / C++ thread类多线程编程
C++ thread类多线程编程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ thread类多线程编程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3989字,纯文字阅读大概需要6分钟。
内容图文
https://blog.csdn.net/dcrmg/article/details/53912941
多线程操作的thread类,简单多线程示例:
#include <iostream> #include <thread> #include <Windows.h> using namespace std; void thread01() { for (int i = 0; i < 5; i++) { cout << "Thread 01 is working !" << endl; Sleep(100); } } void thread02() { for (int i = 0; i < 5; i++) { cout << "Thread 02 is working !" << endl; Sleep(200); } } int main() { thread task01(thread01); thread task02(thread02); task01.join(); task02.join(); for (int i = 0; i < 5; i++) { cout << "Main thread is working !" << endl; Sleep(200); } system("pause"); }
输出:
Thread 02 is working !Thread 01 is working !
Thread 01 is working !
Thread 02 is working !
Thread 01 is working !
Thread 01 is working !
Thread 02 is working !
Thread 01 is working !
Thread 02 is working !
Thread 02 is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
Main thread is working !
请按任意键继续. . .
两个子线程并行执行,join函数会阻塞主流程,所以子线程都执行完成之后才继续执行主线程。可以使用detach将子线程从主流程中分离,独立运行,不会阻塞主线程:
#include <iostream> #include <thread> #include <Windows.h> using namespace std; void thread01() { for (int i = 0; i < 5; i++) { cout << "Thread 01 is working !" << endl; Sleep(100); } } void thread02() { for (int i = 0; i < 5; i++) { cout << "Thread 02 is working !" << endl; Sleep(200); } } int main() { thread task01(thread01); thread task02(thread02); task01.detach(); task02.detach(); for (int i = 0; i < 5; i++) { cout << "Main thread is working !" << endl; Sleep(200); } system("pause"); }
输出:
Main thread is working !
Thread 01 is working !Thread 02 is working !
Thread 01 is working !
Thread 02 is working !Main thread is working !
Thread 01 is working !
Thread 01 is working !
Thread 02 is working !Main thread is working !
Thread 01 is working !
Main thread is working !Thread 02 is working !
Main thread is working !Thread 02 is working !
请按任意键继续. . .
用detach的主线程和两个子线程并行执行。
在绑定的时候也可以同时给带参数的线程传入参数:
#include <iostream> #include <thread> #include <Windows.h> using namespace std; //定义带参数子线程 void thread01(int num) { for (int i = 0; i < num; i++) { cout << "Thread 01 is working !" << endl; Sleep(100); } } void thread02(int num) { for (int i = 0; i < num; i++) { cout << "Thread 02 is working !" << endl; Sleep(200); } } int main() { thread task01(thread01, 5); //带参数子线程 thread task02(thread02, 5); task01.detach(); task02.detach(); for (int i = 0; i < 5; i++) { cout << "Main thread is working !" << endl; Sleep(200); } system("pause"); }
输出跟上例输出一样
多线程数据竞争
多个线程同时对同一变量进行操作的时候,如果不对变量做一些保护处理,有可能导致处理结果异常:
#include <iostream> #include <thread> #include <Windows.h> using namespace std; int totalNum = 100; void thread01() { while (totalNum > 0) { cout << totalNum << endl; totalNum--; Sleep(100); } } void thread02() { while (totalNum > 0) { cout << totalNum << endl; totalNum--; Sleep(100); } } int main() { thread task01(thread01); thread task02(thread02); task01.detach(); task02.detach(); system("pause"); }
有两个问题,一是有很多变量被重复输出了,而有的变量没有被输出;二是正常情况下每个线程输出的数据后应该紧跟一个换行符,但这里大部分却是另一个线程的输出。
这是由于第一个线程对变量操作的过程中,第二个线程也对同一个变量进行各操作,导致第一个线程处理完后的输出有可能是线程二操作的结果。针对这种数据竞争的情况,可以使用线程互斥对象mutex保持数据同步。
mutex类的使用需要包含头文件mutex:
#include <iostream> #include <thread> #include <Windows.h> #include <mutex> using namespace std; mutex mu; //线程互斥对象 int totalNum = 100; void thread01() { while (totalNum > 0) { mu.lock(); //同步数据锁 cout << totalNum << endl; totalNum--; Sleep(100); mu.unlock(); //解除锁定 } } void thread02() { while (totalNum > 0) { mu.lock(); cout << totalNum << endl; totalNum--; Sleep(100); mu.unlock(); } } int main() { thread task01(thread01); thread task02(thread02); task01.detach(); task02.detach(); system("pause"); }
多线程中加入mutex互斥对象之后输出正常。
内容总结
以上是互联网集市为您收集整理的C++ thread类多线程编程全部内容,希望文章能够帮你解决C++ thread类多线程编程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。