C++11 并發(fā)指南之多線程初探
C++11 自2011年發(fā)布以來(lái)已經(jīng)快兩年了,之前一直沒怎么關(guān)注,直到最近幾個(gè)月才看了一些 C++11 的新特性,今后幾篇博客我都會(huì)寫一些關(guān)于 C++11 的特性,算是記錄一下自己學(xué)到的東西吧,和大家共勉。
相信 Linux 程序員都用過(guò) Pthread, 但有了 C++11 的 std::thread 以后,你可以在語(yǔ)言層面編寫多線程程序了,直接的好處就是多線程程序的可移植性得到了很大的提高,所以作為一名 C++ 程序員,熟悉 C++11 的多線程編程方式還是很有益處的。
如果你對(duì) C++11 不太熟悉,建議先看看維基百科上關(guān)于 C++11 新特性的介紹,中文C++11介紹,英文C++11介紹 ,另外C++之父 Bjarne Stroustrup 的關(guān)于 C++11 的 FAQ 也是必看的,我也收集了一些關(guān)于C++11的資料,供大家查閱:
資料匯
http://www.open-std.org/jtc1/sc22/wg21/
C++0x/C++11 Support in GCC:http://gcc.gnu.org/projects/cxx0x.html
What is C++0x:https://www2.research.att.com/~bs/what-is-2009.pdf
Overview of the New C++:http://www.artima.com/shop/overview_of_the_new_cpp
Overview of the New C++ (C++0x).pdf:http://ishare.iask.sina.com.cn/f/20120005.html?from=like
A Brief Look at C++0x:http://www.artima.com/cppsource/cpp0x.html
Summary of C++11 Feature Availability in gcc and MSVC:http://www.aristeia.com/C++11/C++11FeatureAvailability.htm
C++ 11: Come Closer:http://www.codeproject.com/Articles/344282/Cplusplus-11-Come-Closer
C++11 threads, locks and condition variables: http://www.codeproject.com/Articles/598695/Cplusplus11-threads-locks-and-condition-variables
Move Semantics and Perfect Forwarding in C++11:http://www.codeproject.com/Articles/397492/Move-Semantics-and-Perfect-Forwarding-in-Cplusplus
http://solarianprogrammer.com/categories/C++11/
C++11 Concurrency:http://www.baptiste-wicht.com/2012/03/cpp11-concurrency-part1-start-threads/
http://www.hpl.hp.com/personal/Hans_Boehm/misc_slides/sfacm-cleaned.pdf
http://en.cppreference.com/w/cpp/thread
http://isocpp.org/blog/2012/12/c11-a-cheat-sheet-alex-sinyakov
The Biggest Changes in C++11:http://blog.smartbear.com/c-plus-plus/the-biggest-changes-in-c11-and-why-you-should-care/
Ten C++11 Features Every C++ Developer Should Use:http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer
C++11 – A Glance [part 1 of n]:http://www.codeproject.com/Articles/312029/Cplusplus11-A-Glance-part-1-of-n
C++11 – A Glance [part 2 of n]:http://www.codeproject.com/Articles/314415/Cplusplus11-A-Glance-part-2-of-n
C++11(及現(xiàn)代C++風(fēng)格)和快速迭代式開發(fā):http://mindhacks.cn/2012/08/27/modern-cpp-practices/
Lambda Functions in C++11 - the Definitive Guide:http://www.cprogramming.com/c++11/c++11-lambda-closures.html
Better types in C++11 - nullptr, enum classes (strongly typed enumerations) and cstdint:http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html
Rvalue-references-and-move-semantics-in-c++11:http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
http://www.gotw.ca/publications/index.htm
http://www.devx.com/SpecialReports/Door/38865
Multi-threading in C++0x:http://accu.org/index.php/journals/1584
C++ 0X feature summary cheat sheat:http://www.iesensor.com/blog/2011/05/31/c-0x-feature-summary-cheat-sheat/
Multithreading in C++0x part 1: Starting Threads:http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-1-starting-threads.html
http://en.cppreference.com/w/cpp/thread
http://www.cplusplus.com/reference/multithreading/
好了,下面來(lái)說(shuō)正題吧 ;-)
與 C++11 多線程相關(guān)的頭文件
C++11 新標(biāo)準(zhǔn)中引入了四個(gè)頭文件來(lái)支持多線程編程,他們分別是<atomic> ,<thread>,<mutex>,<condition_variable>和<future>。
- <atomic>:該頭文主要聲明了兩個(gè)類, std::atomic 和 std::atomic_flag,另外還聲明了一套 C 風(fēng)格的原子類型和與 C 兼容的原子操作的函數(shù)。
- <thread>:該頭文件主要聲明了 std::thread 類,另外 std::this_thread 命名空間也在該頭文件中。
- <mutex>:該頭文件主要聲明了與互斥量(mutex)相關(guān)的類,包括 std::mutex 系列類,std::lock_guard, std::unique_lock, 以及其他的類型和函數(shù)。
- <condition_variable>:該頭文件主要聲明了與條件變量相關(guān)的類,包括 std::condition_variable 和 std::condition_variable_any。
- <future>:該頭文件主要聲明了 std::promise, std::package_task 兩個(gè) Provider 類,以及 std::future 和 std::shared_future 兩個(gè) Future 類,另外還有一些與之相關(guān)的類型和函數(shù),std::async() 函數(shù)就聲明在此頭文件中。
std::thread "Hello world"
下面是一個(gè)最簡(jiǎn)單的使用 std::thread 類的例子:
#include <stdio.h> #include <stdlib.h> #include <iostream> // std::cout #include <thread> // std::thread void thread_task() { std::cout << "hello thread" << std::endl; } /* * === FUNCTION ========================================================= * Name: main * Description: program entry routine. * ======================================================================== */ int main(int argc, const char *argv[]) { std::thread t(thread_task); t.join(); return EXIT_SUCCESS; } /* ---------- end of function main ---------- */
Makefile 如下:
all:Thread CC=g++ CPPFLAGS=-Wall -std=c++11 -ggdb LDFLAGS=-pthread Thread:Thread.o $(CC) $(LDFLAGS) -o $@ $^ Thread.o:Thread.cc $(CC) $(CPPFLAGS) -o $@ -c $^ .PHONY: clean clean: rm Thread.o Thread
注意在 Linux GCC4.6 環(huán)境下,編譯時(shí)需要加 -pthread,否則執(zhí)行時(shí)會(huì)出現(xiàn):
$ ./Thread terminate called after throwing an instance of 'std::system_error' what(): Operation not permitted Aborted (core dumped)
原因是 GCC 默認(rèn)沒有加載 pthread 庫(kù),據(jù)說(shuō)在后續(xù)的版本中可以不用在編譯時(shí)添加 -pthread 選項(xiàng)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入C++浮點(diǎn)數(shù)無(wú)效值定義與判定的解決辦法
本篇文章是對(duì)C++中浮點(diǎn)數(shù)無(wú)效值定義與判定進(jìn)行了介紹,需要的朋友參考下2013-05-05Qt實(shí)現(xiàn)高準(zhǔn)確率的語(yǔ)音識(shí)別
Vosk是一個(gè)開源的語(yǔ)音識(shí)別工具,支持中英文及多種語(yǔ)言,具備離線識(shí)別能力,且不依賴互聯(lián)網(wǎng),本文就來(lái)聊聊如何使用Vosk API在C++中進(jìn)行中英文識(shí)別吧2024-11-11關(guān)于VS+QT5應(yīng)用程序換圖標(biāo)的解決方案
這篇文章主要介紹了VS+QT5應(yīng)用程序換圖標(biāo)的處理方案,本文給大家提供了兩種解決方案供大家參考,每種方法給大家講解的都非常詳細(xì),需要的朋友可以參考下2021-12-12C語(yǔ)言基礎(chǔ)函數(shù)用法示例詳細(xì)解析
最接地氣的C函數(shù)基礎(chǔ)介紹,此處對(duì)于函數(shù)的相關(guān)知識(shí)點(diǎn)做一些簡(jiǎn)要的介紹,作者實(shí)屬初學(xué),寫博客也是作者學(xué)習(xí)的一個(gè)過(guò)程,難免文章中有內(nèi)容理解不到位或者有不當(dāng)之處,還請(qǐng)朋友們不吝指正2021-11-11C語(yǔ)言 棧的表示和實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了C語(yǔ)言 棧的表示和實(shí)現(xiàn)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12C++多態(tài)特性之派生與虛函數(shù)與模板詳細(xì)介紹
這篇文章主要介紹了C++多態(tài)的特性派生與虛函數(shù)與模板,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09C++中設(shè)計(jì)一個(gè)類時(shí)的注意事項(xiàng)分享
這篇文章主要來(lái)和大家分享一下C++中,設(shè)計(jì)一個(gè)類要注意哪些東西,這往往也是C++面試時(shí)會(huì)考到的問題,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06