C++11 线程
来源:互联网 发布:python开发网站学多久 编辑:程序博客网 时间:2024/06/02 07:53
C++11 线程
flyfish
下面示例包括
1 启动一个线程
2 带参数的线程
3 线程中使用了异步模式
4 使用lambda表达式向窗口发送消息
void f0(){ std::this_thread::sleep_for(std::chrono::seconds(3)); //输出log 表明运行到这里}void f1(int t){ std::this_thread::sleep_for(std::chrono::seconds(3)); //输出t的值}void f2(){ std::future<int> future = std::async(std::launch::async, [](){ std::this_thread::sleep_for(std::chrono::seconds(3)); int ret = 2; return ret; }); std::future_status status; do { status = future.wait_for(std::chrono::seconds(1)); if (status == std::future_status::deferred) { } else if (status == std::future_status::timeout) { } else if (status == std::future_status::ready) { } } while (status != std::future_status::ready); //future.get() 输出2}
使用方式
std::thread t0(f0);//不带参数的线程 t0.detach(); std::thread t1(f1,1);//带参数的线程 t1.detach(); std::thread t2(f2);//线程中的又启动了异步模式 t2.detach();
向对话框发送消息
对话框类头文件
afx_msg LRESULT OnFunction(WPARAM, LPARAM);
实现文件
消息映射
ON_MESSAGE(WM_USER+100, OnFunction)LRESULT 对话框类::OnFunction(WPARAM w, LPARAM l){ AfxMessageBox(L"收到自定义消息"); return 0;}
使用方式
int i = 3; std::thread t3([i,this]() //lambda { std::this_thread::sleep_for(std::chrono::seconds(i)); SendMessage(WM_USER + 100, 0, 0); }); t3.detach();
0 0
- 【C#】线程与线程
- C -线程
- c++/c实现线程池
- 【C/C++】什么是线程安全
- C#lock线程同步
- c下的线程
- ThreadPool 线程池[c#]
- c#线程与枚举
- ThreadPool 线程池[c#]
- c# 实现线程
- ThreadPool 线程池[c#]
- linux C 线程学习
- c实现线程池
- C#|线程初体会
- linux c 线程锁
- 线程安全C/C++
- Linux C 线程(一)
- pthread_create用法 c线程
- java基础知识学习笔记(二)
- 最大流算法(网络流问题)
- Java学习笔记2-JDBC存储过程
- SDK更新失败无法翻墙?这是最笨的办法-仅供娱乐,请勿模仿
- Linux命令之压缩解压缩
- C++11 线程
- 可变参数(...)学习笔记
- 最小生成树算法
- java对象属性和方法的引用
- 【Western Digital】ARCO流程之6A分析
- Cocopods安装与使用,顺带出的几个问题提一下
- hdu5651(乘法逆元&&组合数)
- 非阻塞connect的实现
- __FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程 )