您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)C++中怎么使用async啟動(dòng)并發(fā)任務(wù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Similar to R.12, which tells you to avoid raw owning pointers, you should also avoid raw threads and raw promises where possible. Use a factory function such as std::async, which handles spawning or reusing a thread without exposing raw threads to your own code.
R.12告訴我們避免原始的所有權(quán)指針,本規(guī)則告訴我們:如果可能應(yīng)該避免原始線程和promise。使用像std::async一樣的工廠函數(shù),它可以可以啟動(dòng)和重新使用線程而不必向外部代碼保護(hù)原始線程。
Example(示例)
int read_value(const std::string& filename)
{
std::ifstream in(filename);
in.exceptions(std::ifstream::failbit);
int value;
in >> value;
return value;
}
void async_example()
{
try {
std::future<int> f1 = std::async(read_value, "v1.txt");
std::future<int> f2 = std::async(read_value, "v2.txt");
std::cout << f1.get() + f2.get() << '\n';
} catch (const std::ios_base::failure& fail) {
// handle exception here
}
}
Unfortunately, std::async is not perfect. For example, it doesn't use a thread pool, which means that it may fail due to resource exhaustion, rather than queuing up your tasks to be executed later. However, even if you cannot use std::async, you should prefer to write your own future-returning factory function, rather than using raw promises.
不幸的是,std::async并不完美。例如,它沒(méi)有使用線程池,這意味著它可能因?yàn)橘Y源枯竭而失敗,而不是將任務(wù)排隊(duì)等候執(zhí)行。然而,即使你不能使用std::async,你還是可以編寫(xiě)自己的返回future的工廠函數(shù),而不是使用原始的promise。
This example shows two different ways to succeed at using std::future, but to fail at avoiding raw std::thread management.
示例代碼演示了兩種不同的成功使用std::future的方式,但是沒(méi)能避免使用原始的std::thread。
void async_example()
{
std::promise<int> p1;
std::future<int> f1 = p1.get_future();
std::thread t1([p1 = std::move(p1)]() mutable {
p1.set_value(read_value("v1.txt"));
});
t1.detach(); // evil
std::packaged_task<int()> pt2(read_value, "v2.txt");
std::future<int> f2 = pt2.get_future();
std::thread(std::move(pt2)).detach();
std::cout << f1.get() + f2.get() << '\n';
}
This example shows one way you could follow the general pattern set by std::async, in a context where std::async itself was unacceptable for use in production.
示例代碼顯示了你可以遵守的,由std::async設(shè)定的通常模式,這種模式可以用于開(kāi)發(fā)過(guò)程中std::async無(wú)法直接使用的情況。
void async_example(WorkQueue& wq)
{
std::future<int> f1 = wq.enqueue([]() {
return read_value("v1.txt");
});
std::future<int> f2 = wq.enqueue([]() {
return read_value("v2.txt");
});
std::cout << f1.get() + f2.get() << '\n';
}
Any threads spawned to execute the code of read_value are hidden behind the call to WorkQueue::enqueue. The user code deals only with future objects, never with raw thread, promise, or packaged_task objects.
任何啟動(dòng)并調(diào)用read_value的線程都被隱藏在WorkQueue::enqueue的調(diào)用后面。用戶代碼智能處理future對(duì)象,永遠(yuǎn)不會(huì)使用原始線程,promise或者打包的task對(duì)象。
上述就是小編為大家分享的C++中怎么使用async啟動(dòng)并發(fā)任務(wù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。