欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++線程安全的隊列你了解嘛

 更新時間:2022年03月10日 10:31:46   作者:吃米飯  
這篇文章主要為大家詳細介紹了C++線程安全的隊列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

在這里插入圖片描述

無界隊列

#include<queue>
#include<mutex>
#include<condition_variable>
#include<optional>
#include<cassert>
#include<thread>
template<typename T,typename Container = std::queue<T>>
class Queue	//無界隊列
{
public:
	Queue() = default;
	~Queue() = default;
	//禁止拷貝和移動,編譯器會自動delete
	/*Queue(const Queue&) = delete;
	Queue(Queue&&) = delete;
	Queue& operator=(const Queue&) = delete;
	Queue& operator=(Queue&&) = delete;*/
	void push(const T& val)
	{
		emplace(val);
	}
	void push(T&& val)
	{
		emplace(std::move(val));
	}
	template<typename...Args>
	void emplace(Args&&...args)
	{
		std::lock_guard lk{ mtx_ };
		q_.push(std::forward<Args>(args)...);
		cv_.notify_one();
	}
	T pop()//阻塞
	{
		std::unique_lock lk{ mtx_ };
		cv_.wait(lk, [this] {return !q_.empty(); });//如果隊列不為空就繼續(xù)執(zhí)行,否則阻塞
		assert(!q_.empty());
		T ret{ std::move_if_noexcept(q_.front()) };
		q_.pop();
		return ret;
	}
	std::optional<T> try_pop()//非阻塞
	{
		std::unique_lock lk{ mtx_ };
		if (q_.empty())return {};
		std::optional<T> ret{ std::move_if_noexcept(q_.front()) };
		q_.pop();
		return ret;
	}
	bool empty()const
	{
		std::lock_guard lk{ mtx_ };
		return q_.empty();
	}
private:
	Container q_;
	mutable std::mutex mtx_;
	std::condition_variable cv_;
};
#include<iostream>
int main()
{
	Queue<int>q;
	std::thread t1(
		[&] {
			for (int i = 0; i < 100; ++i)
			{
				q.push(i);
			}
		});
	std::thread t2(
		[&] {
			for (int i = 0; i < 100; ++i)
			{
				//std::cout<<q.pop()<<" ";
				if (auto ret = q.try_pop())
				{
					std::cout << *ret<<" ";
				}
			}
		});
	t1.join();
	t2.join();
	return 0;
}

有界隊列

#include<mutex>
#include<condition_variable>
#include<boost/circular_buffer.hpp>
#include<optional>
template<typename T>
class Queue
{
public:
	Queue(size_t capacity) :q_{ capacity }{}
	template<typename T>
	void push(T&& val)//阻塞
	{
		std::unique_lock lk{ mtx_ };
		not_full_.wait(lk, [this] {return !q_.full(); });
		assert(!q_.full());
		q_.push_back(std::move(std::forward<T>(val)));
		not_empty_.notify_one();
	}
	template<typename T>
	bool try_push(T&& val)//非阻塞
	{
		std::lock_guard lk{ mtx_ };
		if (q_.full())return false;
		q_.push_back(std::forward<T>(val));
		not_empty_.notify_one();
		return true;
	}
	T pop()//阻塞
	{
		std::unique_lock lk{ mtx_ };
		not_empty_.wait(lk, [this] {return !q_.empty(); });
		asert(!q_.empty());
		T ret{ std::move_if_noexcept(q_.front()) };
		q_.pop_front();
		not_full_.notify_one();
		return ret;
	}
	std::optional<T> try_pop()//非阻塞
	{
		std::lock_guard lk{ mtx_ };
		if (q_.empty())return {};
		std::optional<T> ret{ std::move_if_noexcept(q_.front()) };
		q_.pop_front();
		not_full_.notify_one();
		return ret;
	}
private:
	boost::circular_buffer<T>q_;
	std::mutex mtx_;
	std::condition_variable not_full_;
	std::condition_variable not_empty_;
};
#include<iostream>
int main()
{
	Queue<int>q(10);
	std::thread t1(
		[&] {
			for (int i = 0; i < 100; ++i)
			{
				q.push(i);
			}
		});
	std::thread t2(
		[&] {
			for (int i = 0; i < 100; ++i)
			{
				//std::cout<<q.pop()<<" ";
				if (auto ret = q.try_pop())
				{
					std::cout << *ret << " ";
				}
			}
		});
	t1.join();
	t2.join();
	return 0;
}

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容! 

相關(guān)文章

  • 使用C++一步步實現(xiàn)俄羅斯方塊后續(xù)

    使用C++一步步實現(xiàn)俄羅斯方塊后續(xù)

    本文主要給大家分享的是作者在使用C++制作俄羅斯方塊小游戲的時候所需要的常用的函數(shù),有需要的小伙伴可以借鑒下,希望大家能夠喜歡。
    2017-12-12
  • 利用C/C++二進制讀寫png文件的方法示例

    利用C/C++二進制讀寫png文件的方法示例

    最近在做項目的時候遇到了這個問題,所以想著總結(jié)下,方法自己和有需要的朋友,下面這篇文章主要介紹了利用C/C++二進制讀寫png文件的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • C語言實現(xiàn)順序表的基本操作的示例詳解

    C語言實現(xiàn)順序表的基本操作的示例詳解

    順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲。本文將通過示例為大家講解一下順序表的基本操作,需要的可以參考一下
    2022-11-11
  • C語言 文件操作解析詳解及實例代碼

    C語言 文件操作解析詳解及實例代碼

    這篇文章主要介紹了C語言 文件操作解析詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • C++實現(xiàn)二叉樹基本操作詳解

    C++實現(xiàn)二叉樹基本操作詳解

    這篇文章主要為大家詳細介紹了C++實現(xiàn)二叉樹基本操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • C++實現(xiàn)LeetCode(38.計數(shù)和讀法)

    C++實現(xiàn)LeetCode(38.計數(shù)和讀法)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(38.計數(shù)和讀法),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言操作符基礎(chǔ)知識詳解

    C語言操作符基礎(chǔ)知識詳解

    這篇文章主要以圖文結(jié)合的方式為大家詳細介紹了C語言位運算基礎(chǔ)知識,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-10-10
  • 深入理解大數(shù)與高精度數(shù)的處理問題

    深入理解大數(shù)與高精度數(shù)的處理問題

    本篇文章是對大數(shù)與高精度數(shù)的處理進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++示例分析內(nèi)聯(lián)函數(shù)與引用變量及函數(shù)重載的使用

    C++示例分析內(nèi)聯(lián)函數(shù)與引用變量及函數(shù)重載的使用

    為了消除函數(shù)調(diào)用的時空開銷,C++ 提供一種提高效率的方法,即在編譯時將函數(shù)調(diào)用處用函數(shù)體替換,類似于C語言中的宏展開。這種在函數(shù)調(diào)用處直接嵌入函數(shù)體的函數(shù)稱為內(nèi)聯(lián)函數(shù)(Inline Function),又稱內(nèi)嵌函數(shù)或者內(nèi)置函數(shù)
    2022-08-08
  • C語言宏函數(shù)container of()簡介

    C語言宏函數(shù)container of()簡介

    這篇文章介紹了C語言宏函數(shù)container of(),對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12

最新評論