C++11多线程编程之初步介绍
来源:互联网 发布:天诚网络 编辑:程序博客网 时间:2024/04/28 11:42
前言:我认为作为一名C++程序员掌握多线程编程还是很有好处的,在C++11出来之后,有一个std::thread库可以供我们使用,来编写多线程程序;
首先我们需要了解C++11中多线程编程需要涉及的标准库
与C++11多线程相关的头文件
C++11新标准一共提供了五个标准库来支持多线程编程,分别是<thread>,<atomic>,<mutex>,<conditinon_variable>and<future>
<thread>:主要声明了std::thread类
<atomic>:主要声明了两个类,std::atomic和std::atomic_flag
<mutex>:主要声明了与互斥量(mutex)相关的类,包括std::mutex,std::lock_guard,std::unique_lock等
<condition_variable>:主要声明了与条件变量相关的类,包括std::condition_variable和std::condition_variable_any
<future>:主要声明std::promise,std::package_task两个Provider类,以及std::future和std::share_future两个Future类
使用thread类的例子
#include <iostream> // std::cout#include <thread> // std::threadvoid thread_task() { std::cout << "hello thread" << std::endl;}int main(int argc, const char *argv[]){ std::thread t(thread_task); t.join(); return EXIT_SUCCESS;}
defaultthread() noexcept;initialization
template <class Fn, class... Args>explicit thread (Fn&& fn, Args&&... args);copythread(const thread&)=deletemovethread(thread&& T) noexcept;
1、默认构造函数,构造一个空的thread对象
2、初始化构造函数,创建一个thread对象,该对象可以被joinable,新产生的线程会调用fn函数,该函数的参数由args给出
3、拷贝构造函数被禁用,意思是thread不能被拷贝构造
4、move构造函数
可被joinable的对象必须在销毁之前jion主线程或者被detached
thread构造函数使用的例子
#include <iostream>#include <utility>#include <thread>#include <chrono>#include <functional>#include <atomic> void f1(int n){ for (int i = 0; i < 5; ++i) { std::cout << "Thread 1 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); }} void f2(int& n){ for (int i = 0; i < 5; ++i) { std::cout << "Thread 2 executing\n"; ++n; std::this_thread::sleep_for(std::chrono::milliseconds(10)); }} int main(){ int n = 0; std::thread t1; // t1 is not a thread std::thread t2(f1, n + 1); // pass by value std::thread t3(f2, std::ref(n)); // pass by reference std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread t2.join(); t4.join(); std::cout << "Final value of n is " << n << '\n';}
注意:t2里面的n+1是f1的参数,为值传递;t3里面的是引用传递需要使用std::ref()函数
可能的结果:Thread 1 executingThread 2 executingThread 1 executingThread 2 executingThread 1 executingThread 2 executingThread 1 executingThread 2 executingThread 2 executingThread 1 executingFinal value of n is 5
move复制操作
movethread& operator=(thread&& T) noexcept;copythread& operator=(const thread&)=delete;
其他常用相关函数
get_id()获取线程编号joinable()判断是否可以joinjoin()加入线程detach()分离线程swap()交换线程native_handle()返回native_handle注意:detach()是将本线程分离出去,允许本线程单独执行,但是当主线程结束的时候,分离出去的子线程有没有结束都会被强制kill
未完待续。。。。。。。。。。
- C++11多线程编程之初步介绍
- 基于windows的多线程编程初步介绍
- Java多线程--初步介绍
- ubuntu 步步为营之C编程初步
- Java多线程编程初步
- Java多线程编程初步
- Java多线程编程初步
- Linux多线程编程初步
- 多线程编程初步
- VC多线程编程初步
- iOS多线程编程之GCD介绍
- Linux多线程编程—初步
- Linux多线程编程(初步)
- Objective-C多线程编程之GCD
- winsock 编程之初步
- Windows编程之初步
- 编程初步之路
- Linux C编程之信号介绍
- linux下删除.swp文件方法
- struts核心组件
- java的Integer的对比问题
- Android 保存图片到系统图库
- 项目中用到的知识(1)
- C++11多线程编程之初步介绍
- android webview获取网页源代码,js执行前后
- codeforces 478C. Table Decorations(规律)
- Python之SMTP发送邮件
- 设计模式
- KafkaOffsetMonitor 监控异常处理(消息堆积)
- CSS 颜色代码大全
- springMVC传递List和数组到后端
- nodejs入门(07)-函数