C++11 多线程 一
2015-11-30 13:34
316 查看
本周决定 搞定c++11 多线程
常规try catch捕捉异常将不起作用 需要存储异常表达式并传递给主线程 下章开始Lock
Threads
std::thread 在<thread>头文件,可以配合常规函数,匿名函数(lambdas),仿函数使用,并支持传递任意类型和个数参数传递给绑定的线程函数#include "stdafx.h" #include <iostream> #include <thread> #include <string> void func(int n1, int n2, std::string str) { std::cout << n1 << n2 << str << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { std::thread t([](){ printf("thread\n" ); });//创建匿名函数线程并<span style="font-family: Arial, Helvetica, sans-serif;">调用函数起始部分</span> t.join();//阻塞主线程,等到 t线程执行完毕 std::thread thread_test(func, rand(), rand(), "abc");//传递任意参数(传值) //std::thread thread_test(func, std::ref(rand()),std::(rand()), "abc");//通过std::ref()来传递引用 thread_test.join(); //thread_test.detach();//主线程和子线程异步执行 此时要注意主线程生命应大于子线程 //t.swap(thread_test);//交换线程handle和id system("pause"); return 0; }
常规try catch捕捉异常将不起作用 需要存储异常表达式并传递给主线程 下章开始Lock
相关文章推荐
- simple C++ vector
- 【C语言提高05】静态存储区
- 成员函数指针 C++ FAQ LITE — Frequently Asked Questions
- C++标准库中队列的应用
- C语言 对字符串进行分割
- 常用排序算法C++实现(堆排序,快速排序,归并排序,基数排序)
- 全面介绍Windows内存管理机制及C++内存分配实例
- C++中#if #ifdef的作用
- C++11学习笔记(四)
- C++ 14
- C++ 17
- C++虚函数的总结
- C++的三个基本特征:封装、继承、多态
- C语言编程优化运行速度
- 基于windows平台,dev-c++编译工具,搭建ffmpeg 开发环境
- 编译原理(八) 算符优先分析法(分析过程的算法和C++实现)
- C++ 11
- 汉诺塔算法的递归与非递归的C以及C++源代码
- c++ 单例模式的浅谈
- C++的编译单元、可执行程序、内部连接与外部连接