日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C++中標(biāo)準(zhǔn)線(xiàn)程庫(kù)的基本使用介紹_C 語(yǔ)言

作者:碼農(nóng)飛飛 ? 更新時(shí)間: 2022-04-12 編程語(yǔ)言

Qt的封裝程度比較高的線(xiàn)程類(lèi)用多了,發(fā)現(xiàn)C++標(biāo)準(zhǔn)庫(kù)里面的線(xiàn)程庫(kù)有些生疏。這里總結(jié)一下C++標(biāo)準(zhǔn)庫(kù)里面的線(xiàn)程相關(guān)內(nèi)容,供大家參考使用。其實(shí)標(biāo)準(zhǔn)C++的線(xiàn)程庫(kù)也是挺好用的。

1.創(chuàng)建線(xiàn)程異步執(zhí)行

我們可以通過(guò)async函數(shù)直接異步創(chuàng)建一個(gè)線(xiàn)程,這種方法相對(duì)來(lái)說(shuō)比較簡(jiǎn)單,線(xiàn)程執(zhí)行的結(jié)果可以直接用future<T>來(lái)進(jìn)行獲取。

#include <iostream>
#include <future>
 
//線(xiàn)程對(duì)應(yīng)的函數(shù)
bool thread_func(int x) {
	return true;
}
int main()
{
	int inputNum = 65547;
	std::future<bool> future = std::async(thread_func, inputNum);
	bool ret = future.get();
	getchar();
}

2.通過(guò)使用互斥鎖防止線(xiàn)程沖突

線(xiàn)程間同步讀取內(nèi)容的話(huà)一般不會(huì)出現(xiàn)線(xiàn)程安全問(wèn)題,但如果線(xiàn)程間同步寫(xiě)同一個(gè)內(nèi)容的話(huà)就容易出現(xiàn)沖突。比如每個(gè)線(xiàn)程執(zhí)行一次,就會(huì)給全局執(zhí)行次數(shù)累加一次,如果多個(gè)線(xiàn)程同時(shí)執(zhí)行操作,在寫(xiě)的時(shí)候沒(méi)有加鎖,這就有可能導(dǎo)致執(zhí)行次數(shù)被重復(fù)累加的情況。

#include <iostream>
#include <thread>
#include <mutex>
std::mutex mtx; 
 
int count=0;
 
void print_block(int n) {
	mtx.lock();
   count++;
	//do somethings
	mtx.unlock();
}
int main()
{
	std::thread thread1(print_block, 50);
	std::thread thread2(print_block, 50);
 
	thread1.join();
	thread2.join();
	getchar();
	return 0;
}

3.采用信號(hào)量控制線(xiàn)程的運(yùn)行

條件變量(condition_variable)用來(lái)控制線(xiàn)程的運(yùn)行,線(xiàn)程啟動(dòng)的時(shí)候如果條件變量等待,會(huì)阻塞線(xiàn)程的運(yùn)行,直到條件變量發(fā)送對(duì)應(yīng)的通知線(xiàn)程才能開(kāi)始運(yùn)行。通過(guò)采用條件變量我們可以控制線(xiàn)程的運(yùn)行,避免線(xiàn)程空運(yùn)行消耗計(jì)算資源。

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
 
std::mutex mtx;
std::condition_variable cv;
 
void print_id(int id) {
	std::unique_lock<std::mutex> lck(mtx);
	cv.wait(lck);
	std::cout << "thread " << id << '\n';
}
void go() {
	std::unique_lock<std::mutex> lck(mtx);
	cv.notify_all();
}
int main()
{
	std::thread threads[10];
	for (int i = 0; i < 10; ++i)
		threads[i] = std::thread(print_id, i);
   std::cout << "start thread run" << std::endl;
	go();
	for (auto& th : threads){th.join();}
	getchar();
	return 0;
}

4.通過(guò)promise實(shí)現(xiàn)進(jìn)程間通信

很多時(shí)候線(xiàn)程間執(zhí)行是有先后順序的,我們需要等待上一個(gè)線(xiàn)程執(zhí)行結(jié)束拿到結(jié)果之后再執(zhí)行當(dāng)前線(xiàn)程,這時(shí)候就涉及到線(xiàn)程間的等待和數(shù)據(jù)傳遞這時(shí)候std::promise<T>就能排上用場(chǎng)了,通過(guò)使用該變量我們可以很輕松的實(shí)現(xiàn)線(xiàn)程間的等待和數(shù)據(jù)傳遞。

#include <iostream>
#include <future>
#include <chrono>
void Thread_Fun1(std::promise<int> &p)
{
	std::this_thread::sleep_for(std::chrono::seconds(5));
	int iVal = 233;
	std::cout << "傳入數(shù)據(jù)(int):" << iVal << std::endl;
	p.set_value(iVal);
}
 
void Thread_Fun2(std::future<int> &f)
{
	//阻塞函數(shù),直到收到相關(guān)聯(lián)的std::promise對(duì)象傳入的數(shù)據(jù)
	auto iVal = f.get();
	std::cout << "收到數(shù)據(jù)(int):" << iVal << std::endl;
}
 
int main()
{
	std::promise<int> pr1;
	std::future<int> fu1 = pr1.get_future();
 
	std::thread t1(Thread_Fun1, std::ref(pr1));
	std::thread t2(Thread_Fun2, std::ref(fu1));
 
	//阻塞至線(xiàn)程結(jié)束
	t1.join();
	t2.join();
	return 1;
}

總結(jié)

原文鏈接:https://blog.csdn.net/yang1fei2/article/details/122795939

欄目分類(lèi)
最近更新