线程
#include<thread>
线程的几个重要函数
thread t(func);
join();
detach();
get_id();
hardware_concurrency();
- 创建一个线程并等待子线程
/*************************************************************************
> File Name: test.cpp
> Author: chudongfang
> Mail:
************************************************************************/
#include<iostream>
#include<thread>
using namespace std;
void func()
{
cout <<" I'm son thread " <<endl;
}
int main()
{
thread t(func);
t.join();
cout <<" I'm father thread " <<endl;
return 0;
}
- 创建一个线程并分离
/*************************************************************************
> File Name: test.cpp
> Author:
> Mail:
************************************************************************/
#include<iostream>
#include<thread>
using namespace std;
void func()
{
cout <<" I'm son thread " <<endl;
}
int main()
{
thread t(func);
//t.join();
t.detach();
cout <<" I'm father thread " <<endl;
return 0;
}
- 信息获取
/*************************************************************************
> File Name: test.cpp
> Author:
> Mail:
************************************************************************/
#include<iostream>
#include<thread>
using namespace std;
void func()
{
cout <<" I'm son thread " <<endl;
}
int main()
{
thread t(func);
cout<< t.get_id() << endl; // 获取线程ID
cout<<thread::hardware_concurrency() << endl; //获取CPU核数
t.join();
//t.detach();
cout <<" I'm father thread " <<endl;
return 0;
}
- 线程睡眠
/*************************************************************************
> File Name: test.cpp
> Author:
> Mail:
************************************************************************/
#include<chrono>
#include<iostream>
#include<thread>
using namespace std;
void func()
{
this_thread::sleep_for(chrono::seconds(3));//线程睡眠3秒
cout <<" I'm son thread " <<endl;
}
int main()
{
thread t(func);
cout<< t.get_id() << endl; // 获取线程ID
cout<<thread::hardware_concurrency() << endl; //获取CPU核数
t.join();
//t.detach();
cout <<" I'm father thread " <<endl;
return 0;
}
互斥量和条件变量
说到线程就一定会有多线程编程,而多线程编程少不了的就是互斥量和条件变量
互斥量
#include<mutex>
常用函数
mutex_.lock();
mutex_.unlock();
mutex_.try_lock();
std::lock_guard<std::mutex> locker(mutex_);
lock_guard可以自动在作用域内析构,可以很好的代替lock && unlock
- lock() && unlock()
/*************************************************************************
> File Name: test.cpp
> Author:
> Mail:
************************************************************************/
#include<chrono>
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
mutex mutex_;
void func()
{
mutex_.lock();
cout << "thread " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::seconds(1));
mutex_.unlock();
}
int main()
{
thread t3(func);
thread t1(func);
thread t2(func);
t1.join();
t2.join();
t3.join();
return 0;
}
- std::lock_guard locker(mutex_);
/*************************************************************************
> File Name: test.cpp
> Author:
> Mail:
************************************************************************/
#include<chrono>
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
mutex mutex_;
void func()
{
std::lock_guard<std::mutex> locker(mutex_);
cout << "thread " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::seconds(1));
}
int main()
{
thread t3(func);
thread t1(func);
thread t2(func);
t1.join();
t2.join();
t3.join();
return 0;
}
条件变量
#include<condition_vatiable>
- condition_vatiable配合 std::unique_lock进行wait操作
- condition_vatiable效率高 ,condition_vatiable_any灵活性高
条件变量使用过程
- 拥有条件变量的线程获取互斥量
- 循环检查某个条件,如果条件不足,则阻塞直到条件满足:如果条件满足,则向下执行.
- 某个线程满足条件执行完后调用notify_one或者notify_all唤醒一个或者所有等待的线程.
这里给出一个同步队列的实现:
//SyncQueue.hpp
#include<list>
#include<mutex>
#include<thread>
#include<condition_variable>
#include<iostream>
template <typename T>
class SyncQueue
{
public:
//构造函数
SyncQueue(int maxSize) : maxSize_(maxSize) , needStop_(false)
{
}
//左值放入
void Put(const T& x)
{
Add(x);
}
//右值引用放入
void Put(T&&x)
{
Add(std::forward<T>(x));
}
void Take(std::list<T>& list)
{
//配合条件变量使用
std::unique_lock<std::mutex> locker(mutex_);
notEmpty_.wait(locker, [this]{ return needStop_ || NotEmpty(); } ); //等待满足条件 + lambda表达式
if(needStop_)
return ;
list = std::move(queue_); // 右值赋值,不需要进行拷贝
notFull_.notify_one();
}
void Take(T& t)
{
std::unique_lock<std::mutex> locker(mutex_);
notEmpty_.wait(locker, [this]{ return needStop_ || NotEmpty(); } );
if(needStop_)
return ;
t = queue_.front(); //左值赋值
queue_.pop_front();
notFull_.notify_one();//唤醒一个等待的线程
}
void Stop()
{
{
std::lock_guard<std::mutex> locker(mutex_);
needStop_ = true;
}
notFull_.notify_all();//唤醒所有的进程
notEmpty_.notify_all();//唤醒所有的进程
}
bool Empty()
{
std::lock_guard<std::mutex> locker(mutex_);
return queue_.empty();
}
bool Full()
{
std::lock_guard<std::mutex> locker(mutex_);
return queue_.size() == maxSize_;
}
size_t Size()
{
std::lock_guard<std::mutex> locker(mutex_);
return queue_.size();
}
/*
int Count()
{
return queue_.size();
}
*/
private:
bool NotFull() const
{
bool full = queue_.size() >= maxSize_;
if(full)
{
std::cout << "The queue is full , Please wait! "<<std::endl;
}
return !full;
}
bool NotEmpty() const
{
bool empty = queue_.empty();
if(empty)
{
std::cout <<"The queue is empty , Please wait !" << std::this_thread::get_id() << std::endl;
}
return !empty;
}
template <typename F>
void Add(F&&x)
{
std::unique_lock<std::mutex> locker(mutex_);
notFull_.wait(locker, [this]{return needStop_ || NotFull();} );
if(needStop_)
return ;
queue_.push_back(std::forward<F>(x));
notEmpty_.notify_one();
}
private:
std::list<T> queue_;
std::mutex mutex_;// 互斥锁
std::condition_variable notEmpty_; //非空条件变量
std::condition_variable notFull_; //非满条件变量
int maxSize_;
bool needStop_;
};