C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准中的一些表示线程的类、用于互斥访问的类与方法等。应用C++11中的std::thread便于多线程程序的移值。
关联头文件
<thread>:该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。
C++11新标准中引入了四个头文件来支持多线程编程,他们分别是<atomic>,<mutex>,<condition_variable>和<future>。
- <atomic>:该头文主要声明了两个类, std::atomic 和std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。
- <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::shared_future 两个 Future类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。
类成员
- get_id:获取线程ID,返回一个类型为std::thread::id的对象。
- joinable:检查线程是否可被合并join。检查thread对象是否标识一个活动(active)的可行性线程。缺省构造的thread对象、已经完成join的thread对象、已经detach的thread对象都不是joinable。
- hardware_concurrency:静态成员函数,返回当前计算机最大的硬件并发线程数目。基本上可以视为处理器CPU的核心数目。
- native_handle:该函数返回与std::thread具体实现相关的线程句柄。native_handle_type是连接thread类和操作系统SDK API之间的桥梁,如在Linux g++(libstdc++)里,native_handle_type其实就是pthread里面的pthread_t类型,当thread类的功能不能满足我们的要求的时候(比如改变某个线程的优先级),可以通过thread类实例的native_handle()返回值作为参数来调用相关的pthread函数达到目录。
- join:调用该函数会阻塞当前线程。阻塞调用者(caller)所在的线程直至被join的std::thread对象标识的线程执行结束。
- detach:将当前线程对象所代表的执行实例与该线程对象分离,使得线程的执行可以单独进行。一旦线程执行完毕,它所分配的资源将会被释放。
- swap:交换二个 thread 对象
- operator=:moves the thread object
另外,std::thread::id表示线程ID,定义了在运行时操作系统内唯一能够标识该线程的标识符,同时其值还能指示所标识的线程的状态。
有时候我们需要在线程执行代码里面对当前调用者线程进行操作,针对这种情况,C++11里面专门定义了一个命名空间this_thread,此命名空间也声明在<thread>头文件中,其中包括
- get_id()函数用来获取当前调用者线程的ID;
- yield()函数可以用来将调用者线程跳出运行状态,重新交给操作系统进行调度,即当前线程放弃执行,操作系统调度另一线程继续执行;
- sleep_until()函数是将线程休眠至某个指定的时刻(time point),该线程才被重新唤醒;
- sleep_for()函数是将线程休眠某个指定的时间片(time span),该线程才被重新唤醒,不过由于线程调度等原因,实际休眠实际可能比sleep_duration所表示的时间片更长。
#include <iostream>
#include <thread>
using namespace std;
int cnt = 20;
void fun1()
{
while (cnt > 0)
{
if(cnt > 0)
{
--cnt;
cout<< cnt << " ";
}
}
}
int main()
{
thread t1(fun1);
thread t2(fun1);
t1.join();
t2.join();
std::cout << "\nhere is the main()" << std::endl;
return 0;
}
//输出,没有按顺序输出
//1918 16 17 15 14 13 12 11 9 10 8 7 6 5 4 3 2 1 0
//here is the main()
mutex和std::lock_guard的使用
头文件是#include <mutex>,mutex是用来保证线程同步的,防止不同的线程同时操作同一个共享数据。
但使用lock_guard则相对安全,它是基于作用域的,能够自解锁,当该对象创建时,它会像m.lock()一样获得互斥锁,当生命周期结束时,它会自动析构(unlock),不会因为某个线程异常退出而影响其他线程。
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int cnt = 20;
mutex m;
void fun1()
{
while (cnt > 0)
{
std::lock_guard<std::mutex> lockGuard(m);
// m.lock();
if (cnt > 0)
{
--cnt;
cout<< cnt << " ";
}
// m.unlock();
}
}
int main()
{
thread t1(fun1);
thread t2(fun1);
t1.join();
t2.join();
std::cout << "\nhere is the main()" << std::endl;
return 0;
}
//输出结果,cnt是依次递减的,没有因为多线程而打乱次序
//19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
//here is the main()