C++11的多線程編程方式
std::thread
在c++11的標(biāo)準(zhǔn)里,線程已經(jīng)由標(biāo)準(zhǔn)庫提供:std::thread,它起源于POSIX thread璃岳,因此在使用std::thread來做線程編程時耕腾,編譯需要帶上-lpthread。
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
void f1(int n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread 1 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void f2(int& n)
{
for (int i = 0; i < 5; ++i) {
std::cout << "Thread 2 executing\n";
++n;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
int main()
{
int n = 0;
std::thread t1; // t1 is not a thread
std::thread t2(f1, n + 1); // pass by value
std::thread t3(f2, std::ref(n)); // pass by reference
std::thread t4(std::move(t3)); // t4 is now running f2(). t3 is no longer a thread
t2.join();
t4.join();
std::cout << "Final value of n is " << n << '\n';
}
future和promise
除了std::thread這種原始的多線程編程方式剃法,c++11還提供了future,promise這種語義的多線程編程方式,future以及promise能很好地處理需要線程之間傳遞數(shù)據(jù)以及同步的情況贷洲。
// promise example
#include <iostream> // std::cout
#include <functional> // std::ref
#include <thread> // std::thread
#include <future> // std::promise, std::future
void print_int (std::future<int>& fut) {
int x = fut.get();
std::cout << "value: " << x << '\n';
}
int main ()
{
std::promise<int> prom; // create promise
std::future<int> fut = prom.get_future(); // engagement with future
std::thread th1 (print_int, std::ref(fut)); // send future to new thread
prom.set_value (10); // fulfill promise
// (synchronizes with getting the future)
th1.join();
return 0;
}
packaged_task
packaged_task是用于將future和promise連接起來的模板類收厨,旨在減少使用future和promise語義編寫多線程程序時的代碼的冗余。
// packaged_task example
#include <iostream> // std::cout
#include <future> // std::packaged_task, std::future
#include <chrono> // std::chrono::seconds
#include <thread> // std::thread, std::this_thread::sleep_for
// count down taking a second for each value:
int countdown (int from, int to) {
for (int i=from; i!=to; --i) {
std::cout << i << '\n';
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << "Lift off!\n";
return from-to;
}
int main ()
{
std::packaged_task<int(int,int)> tsk (countdown); // set up packaged_task
std::future<int> ret = tsk.get_future(); // get future
std::thread th (std::move(tsk),10,0); // spawn thread to count down from 10 to 0
// ...
int value = ret.get(); // wait for the task to finish and get result
std::cout << "The countdown lasted for " << value << " seconds.\n";
th.join();
return 0;
}
async
async提供了最簡單的并發(fā)編程方式优构,使用async進(jìn)行并行編程無需考慮線程和鎖诵叁,在調(diào)用async時,該任務(wù)應(yīng)該不包含有需要鎖保護(hù)的共享數(shù)據(jù)钦椭,而async會根據(jù)當(dāng)前cpu的core的使用情況來決定創(chuàng)建多少個thread來運(yùn)行該任務(wù)拧额。
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <future>
template <typename RAIter>
int parallel_sum(RAIter beg, RAIter end)
{
auto len = end - beg;
if(len < 1000)
return std::accumulate(beg, end, 0);
RAIter mid = beg + len/2;
auto handle = std::async(std::launch::async,
parallel_sum<RAIter>, mid, end);
int sum = parallel_sum(beg, mid);
return sum + handle.get();
}
int main()
{
std::vector<int> v(10000, 1);
std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
}
c++11控制并發(fā)邏輯的方式
future和promise
這種方式主要是通過這種語義實現(xiàn)程序上的多線程的并發(fā)邏輯的控制,具體參考上面的程序彪腔。
鎖與條件變量
鎖是一個經(jīng)典的概念侥锦,經(jīng)常被用于控制多線程訪問共享數(shù)據(jù)的邏輯,條件變量更是提供了同步多個線程的語義德挣。條件變量的必須和鎖——跟更確地是std::unique_lock<std::mutex>——配合使用恭垦,
這種配合使用的限制可以使得條件變量在一些平臺上的效率得到最大的優(yōu)化。
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;
void worker_thread()
{
// Wait until main() sends data
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return ready;});
// after the wait, we own the lock.
std::cout << "Worker thread is processing data\n";
data += " after processing";
// Send data back to main()
processed = true;
std::cout << "Worker thread signals data processing completed\n";
// Manual unlocking is done before notifying, to avoid waking up
// the waiting thread only to block again (see notify_one for details)
lk.unlock();
cv.notify_one();
}
int main()
{
std::thread worker(worker_thread);
data = "Example data";
// send data to the worker thread
{
std::lock_guard<std::mutex> lk(m);
ready = true;
std::cout << "main() signals data ready for processing\n";
}
cv.notify_one();
// wait for the worker
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk, []{return processed;});
}
std::cout << "Back in main(), data = " << data << '\n';
worker.join();
}
std::lock_guard和std::unique_lock類似格嗅,但是前者在構(gòu)造的時候就會加鎖番挺,后者可以不用在構(gòu)造時就立即加鎖(默認(rèn)構(gòu)造不需要鎖),加鎖可以發(fā)生在其生命期間的任何時刻屯掖,另外后者也可以轉(zhuǎn)移其所有權(quán)到別的變量玄柏。
條件變量的notify方法能用于線程的喚醒,當(dāng)有notify發(fā)生時贴铜,其他處于waiting狀態(tài)的線程會嘗試獲取鎖粪摘,成功后判斷需要喚醒的條件(通過wait的第二個參數(shù)給出)是否為真,如果是真就喚醒阀湿,否則釋放鎖繼續(xù)waiting赶熟。
一個充分使用了c++11特性的線程池
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this]
{
for(;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
task();
}
}
);
}
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return res;
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for(std::thread &worker: workers)
worker.join();
}
#endif
該線程池主要是維護(hù)了一個任務(wù)的隊列,初始化好的線程不斷從該隊列中取出任務(wù)并執(zhí)行陷嘴,同時維護(hù)好隊列。
一些陷阱
std::vector<std::future<std::pair<std::string, std::string> > > results;
results.emplace_back(thread_pool->enqueue([](){return std::make_pair();}));
for (auto && res: results) {
auto & ret = res.get();
auto t = std::thread(somefunc, std::ref(ret.first), std::ref(ret.second));
t.detach();
}
這里的問題在于ret.first, ret.second會在不同線程里使用同一個對象的引用间坐,其值在不同線程里都一樣灾挨,這明顯有悖初衷。修改的方法簡單直接:使用值傳遞竹宋,而不是使用引用劳澄。