一、概述
在C++中,多線程編程已成為常見的編程模式。然而,多線程編程有一個較為困難的問題:如何保證多個線程的執行順序,確保某個線程在需要時能夠等待其他線程的結束?本文將介紹一些技巧和方法,以解決這些問題,使得多個線程能夠協調有序地執行。
二、使用std::thread::join方法等待線程結束
C++11中引入了std::thread類,使得線程的創建和管理變得更加容易。其中,join方法可用來等待線程結束。該方法將阻塞直到線程運行結束為止。下面是一個例子:
#include <iostream>
#include <thread>
void threadFunc()
{
std::cout << "thread function executing" << std::endl;
}
int main()
{
std::thread t(threadFunc);
std::cout << "main thread executing" << std::endl;
t.join();
std::cout << "thread terminated" << std::endl;
}
在該例子中,首先創建了一個線程t,並調用了它的join方法。在調用join方法之前,線程t的函數threadFunc被執行。join方法的執行將阻塞當前線程(即主線程),直到線程t執行結束。線程t結束後,程序繼續執行,輸出 “thread terminated”。
需要注意的是,每個線程只能被join一次,第二次調用join方法將導致未定義行為。
三、使用std::thread::detach方法將線程和主線程分離
另外一種等待線程結束的方法是使用std::thread::detach方法。當線程被分離後,其生命周期將不再由主線程控制。該方法的調用將立即返回,不會阻塞當前線程。下面是一個例子:
#include <iostream>
#include <thread>
void threadFunc()
{
std::cout << "thread function executing" << std::endl;
}
int main()
{
std::thread t(threadFunc);
std::cout << "main thread executing" << std::endl;
t.detach();
std::cout << "thread detached" << std::endl;
return 0;
}
在該例子中,線程t被創建,然後調用detach方法將其分離。由於線程t已經被分離,不再受主線程的控制,因此主線程在完成自身任務後,將立即退出。因此,在該例子中並沒有輸出 “thread function executing”。
但是需要注意的是,當線程被分離後,它的資源(例如堆棧)將不再被自動釋放,必須手動釋放。
四、使用std::promise和std::future
如果需要某個線程的結果數據,可以使用std::promise和std::future來實現等待線程結束並獲取結果的效果。std::promise是一個可以與std::future配合使用的類模板,它的作用是給線程一個「承諾」,立即返回一個與之相關聯的std::future對象。std::future對象是一個可以用來訪問「承諾」的對象,它提供了等待「承諾」完成並獲取結果的方法。
下面是一個例子:
#include <iostream>
#include <thread>
#include <future>
void threadFunc(std::promise<int>& p)
{
std::cout << "thread function executing" << std::endl;
p.set_value(42);
}
int main()
{
std::promise<int> p;
std::future<int> f = p.get_future();
std::thread t(threadFunc, std::ref(p));
std::cout << "main thread executing" << std::endl;
int result = f.get();
std::cout << "result=" << result << std::endl;
t.join();
return 0;
}
在該例子中,首先創建了一個std::promise對象p,並調用它的get_future方法得到一個std::future對象f。然後創建了一個線程t,並將p作為參數傳給線程。在線程函數threadFunc中,將結果值42存入p,主線程調用f的get方法等待線程t的結果。當線程t執行結束後,程序繼續執行,輸出結果。
五、使用std::condition_variable等待線程結束
std::condition_variable是一個同步原語,可以用來在多個線程之間進行等待和通知。當條件不滿足時,線程可以通過wait方法阻塞;當條件滿足時,線程可以通過notify_one或notify_all方法進行通知。使用std::condition_variable可以等待多個線程的運行結束,從而達到等待多個線程結束的效果。
下面是一個例子:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
std::mutex mtx;
std::condition_variable cv;
void threadFunc(int id)
{
std::unique_lock<std::mutex> lck(mtx);
std::cout << "thread " << id << " executing" << std::endl;
cv.notify_one();
}
int main()
{
std::vector<std::thread> threads;
for (int i = 0; i < 5; ++i)
{
threads.emplace_back(threadFunc, i + 1);
}
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [&]() { return threads.empty(); });
std::cout << "all threads terminated" << std::endl;
return 0;
}
在該例子中,首先創建了5個線程,並將它們的ID作為參數傳遞給線程函數threadFunc。在線程函數中,輸出線程的ID並通知等待的線程。在主線程中,等待所有線程執行結束。在等待的過程中,主線程通過wait方法阻塞,直到所有線程都執行結束,等待期間condition_variable會釋放鎖。當條件滿足時,主線程繼續執行,輸出 “all threads terminated”。
六、總結
本文介紹了多種等待C++多線程結束的方法,包括使用std::thread::join方法、使用std::thread::detach方法、使用std::promise和std::future以及使用std::condition_variable等待線程結束。選擇合適的方法可以使得多線程編程更加高效而容易。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/231994.html