C++11 Concurrency Guide One (c++11 multithreading approach)

Source: Internet
Author: User
Tags mutex thread class

Introduction

C++11 since 2011 release has been nearly two years, has not been much attention, until the last few months to see some of the new features of C++11, in the future several blog I will write some about the characteristics of c++11, is a record of what I learned, and everyone to encourage.

Believe that the Linux programmers have used Pthread, but with C++11 Std::thread later, you can write multithreaded programs at the language level, the direct benefit is that the portability of multithreaded programs has been greatly improved, so as a C + + programmer, familiar with c++11 multi-line Programming method is also very useful.

If you are not familiar with the c++11, it is recommended to take a look at Wikipedia on the new features of c++11 Introduction, Chinese c++11 Introduction, English c++11 Introduction, and the father of C + + Bjarne Stroustrup about c++11 FAQ is also must see, I also collected some about c+ +11 of the information for everyone to access:

data Exchange

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 (and modern C + + style) and rapid iterative development: 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/

OK, here's the point;-)

header files related to c++11 multithreading

The c++11 new standard introduces four header files to support multithreaded programming, respectively <atomic>,<thread>,<mutex>,<condition_variable> and < Future>.

    • <atomic>: This article mainly declares two classes, std::atomic and Std::atomic_flag, and also declares a set of C-style atomic types and functions that are compatible with C atomic operations.
    • <thread>: The header file mainly declares the Std::thread class, and the Std::this_thread namespace is also in the header file.
    • <mutex>: The header file primarily declares classes related to mutexes (mutexes), including Std::mutex series classes, Std::lock_guard, Std::unique_lock, and other types and functions.
    • <condition_variable>: The header file primarily declares classes related to condition variables, including std::condition_variable and Std::condition_variable_any.
    • <future>: This header file mainly declares std::p romise, std::p ackage_task Two Provider classes, and Std::future and Std::shared_future two future classes, plus There are also types and functions associated with it, and the Std::async () function is declared in this header file.
Std::thread "Hello World"

Here is one of the simplest examples of using the Std::thread class

1#include <stdio.h>2#include <stdlib.h>3 4#include <iostream>//Std::cout5#include <thread>//Std::thread6 7 voidThread_task () {8Std::cout <<"Hello thread"<<Std::endl;9 }Ten  One /* A * = = FUNCTION ========================================================= - * Name:main - * Description:program entry routine. the  * ======================================================================== -  */ - intMainintargcConst Char*argv[]) - { + std::thread T (thread_task); - T.join (); +  A     returnexit_success; at}/*----------End of function main----------*/

Makefile as follows:

ALL:THREADCC=G++CPPFLAGS=-WALL-STD=C++11-GGDBLDFLAGS=-PTHREADTHREAD:THREAD.O    $ (CC) $ (LDFLAGS)-O [email Protected] $^thread.o:thread.cc    $ (cc) $ (cppflags)-o [email protected]-C $^. Phony:    Cleanclean:    rm thread.o Thread

Note that in a Linux GCC4.6 environment, you need to add-pthread at compile time, otherwise the execution will appear:

$./threadterminate called after throwing a instance of ' Std::system_error ' What  ():  Operation not Permittedaborted (Core dumped)

The reason is that GCC does not load the Pthread library by default, and it is said that in subsequent releases it is not possible to add the-pthread option at compile time.

Transferred from: http://www.cnblogs.com/haippy/p/3235560.html

C++11 Concurrency Guide One (c++11 multithreading) (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.